detect portrait mode on mobile so it doesnt go into two col layout when keyboard is up, (#178)

This commit is contained in:
Ginger Wong 2020-09-22 17:09:55 -07:00
parent 17ef8eed0d
commit 01f956d043
5 changed files with 28 additions and 13 deletions

View File

@ -7,7 +7,7 @@ import SocialIcon from './components/social.js';
import UsernameForm from './components/chat/username.js'; import UsernameForm from './components/chat/username.js';
import Chat from './components/chat/chat.js'; import Chat from './components/chat/chat.js';
import Websocket from './utils/websocket.js'; import Websocket from './utils/websocket.js';
import { secondsToHMMSS, hasTouchScreen } from './utils/helpers.js'; import { secondsToHMMSS, hasTouchScreen, getOrientation } from './utils/helpers.js';
import { import {
addNewlines, addNewlines,
@ -35,6 +35,7 @@ import {
URL_OWNCAST, URL_OWNCAST,
URL_STATUS, URL_STATUS,
WIDTH_SINGLE_COL, WIDTH_SINGLE_COL,
ORIENTATION_PORTRAIT,
} from './utils/constants.js'; } from './utils/constants.js';
export default class App extends Component { export default class App extends Component {
@ -42,6 +43,7 @@ export default class App extends Component {
super(props, context); super(props, context);
const chatStorage = getLocalStorage(KEY_CHAT_DISPLAYED); const chatStorage = getLocalStorage(KEY_CHAT_DISPLAYED);
this.hasTouchScreen = hasTouchScreen();
this.state = { this.state = {
websocket: new Websocket(), websocket: new Websocket(),
@ -67,10 +69,9 @@ export default class App extends Component {
// dom // dom
windowWidth: window.innerWidth, windowWidth: window.innerWidth,
windowHeight: window.innerHeight, windowHeight: window.innerHeight,
orientation: getOrientation(this.hasTouchScreen),
}; };
this.hasTouchScreen = hasTouchScreen();
// timers // timers
this.playerRestartTimer = null; this.playerRestartTimer = null;
this.offlineTimer = null; this.offlineTimer = null;
@ -344,6 +345,7 @@ export default class App extends Component {
this.setState({ this.setState({
windowWidth: window.innerWidth, windowWidth: window.innerWidth,
windowHeight: window.innerHeight, windowHeight: window.innerHeight,
orientation: getOrientation(this.hasTouchScreen),
}); });
} }
@ -353,6 +355,7 @@ export default class App extends Component {
configData, configData,
displayChat, displayChat,
extraUserContent, extraUserContent,
orientation,
overallMaxViewerCount, overallMaxViewerCount,
playerActive, playerActive,
sessionMaxViewerCount, sessionMaxViewerCount,
@ -366,6 +369,7 @@ export default class App extends Component {
windowWidth, windowWidth,
} = state; } = state;
const { const {
version: appVersion, version: appVersion,
logo = {}, logo = {},
@ -409,7 +413,8 @@ export default class App extends Component {
const mainClass = playerActive ? 'online' : ''; const mainClass = playerActive ? 'online' : '';
const streamInfoClass = streamOnline ? 'online' : ''; // need? const streamInfoClass = streamOnline ? 'online' : ''; // need?
const shortHeight = windowHeight <= HEIGHT_SHORT_WIDE; const isPortrait = this.hasTouchScreen && orientation === ORIENTATION_PORTRAIT;
const shortHeight = windowHeight <= HEIGHT_SHORT_WIDE && !isPortrait;
const singleColMode = windowWidth <= WIDTH_SINGLE_COL && !shortHeight; const singleColMode = windowWidth <= WIDTH_SINGLE_COL && !shortHeight;
const extraAppClasses = classNames({ const extraAppClasses = classNames({

View File

@ -127,15 +127,12 @@ export default class Chat extends Component {
} }
websocketConnected() { websocketConnected() {
console.log("=======socket connected.")
this.setState({ this.setState({
webSocketConnected: true, webSocketConnected: true,
}); });
} }
websocketDisconnected() { websocketDisconnected() {
console.log("=======socket not connected.")
this.setState({ this.setState({
webSocketConnected: false, webSocketConnected: false,
}); });

View File

@ -51,3 +51,5 @@ export const MESSAGE_JUMPTOBOTTOM_BUFFER = 260;
// app styling // app styling
export const WIDTH_SINGLE_COL = 730; export const WIDTH_SINGLE_COL = 730;
export const HEIGHT_SHORT_WIDE = 500; export const HEIGHT_SHORT_WIDE = 500;
export const ORIENTATION_PORTRAIT = 'portrait';
export const ORIENTATION_LANDSCAPE = 'landscape';

View File

@ -1,3 +1,5 @@
import { ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT } from './constants.js';
export function getLocalStorage(key) { export function getLocalStorage(key) {
try { try {
return localStorage.getItem(key); return localStorage.getItem(key);
@ -75,6 +77,21 @@ export function hasTouchScreen() {
return hasTouch; return hasTouch;
} }
export function getOrientation(forTouch = false) {
// chrome mobile gives misleading matchMedia result when keyboard is up
if (forTouch && window.screen && window.screen.orientation) {
return window.screen.orientation.type.match('portrait') ?
ORIENTATION_PORTRAIT :
ORIENTATION_LANDSCAPE;
} else {
// all other cases
return window.matchMedia("(orientation: portrait)").matches ?
ORIENTATION_PORTRAIT :
ORIENTATION_LANDSCAPE;
}
}
// generate random avatar from https://robohash.org // generate random avatar from https://robohash.org
export function generateAvatar(hash) { export function generateAvatar(hash) {
const avatarSource = 'https://robohash.org/'; const avatarSource = 'https://robohash.org/';

View File

@ -132,12 +132,6 @@ header {
min-height: auto; min-height: auto;
} }
.short-wide #message-input {
height: 3rem;
}
/* *********** single col layout ***************************** */ /* *********** single col layout ***************************** */