Skip to content

Commit 0770400

Browse files
committed
mq_stuff
1 parent 19802fd commit 0770400

File tree

7 files changed

+74
-36
lines changed

7 files changed

+74
-36
lines changed

packages/common/src/utils.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import {
2323
isAndroid,
2424
isIOS,
2525
WINDOWS_EMOJI_FALLBACK_FONT,
26+
MQ_MAX_HEIGHT_LANDSCAPE,
27+
MQ_MAX_MOBILE,
28+
MQ_MAX_TABLET,
29+
MQ_MAX_WIDTH_LANDSCAPE,
30+
MQ_MIN_TABLET,
2631
} from "./constants";
2732

2833
import type { MaybePromise, ResolutionType } from "./utility-types";
@@ -1324,3 +1329,34 @@ export const isMobileOrTablet = (): boolean => {
13241329
}
13251330
return false;
13261331
};
1332+
1333+
export const getUIMode = ({
1334+
width,
1335+
height,
1336+
}: {
1337+
width: number;
1338+
height: number;
1339+
}) => {
1340+
return isTabletBreakpoint(width, height) && isMobileOrTablet()
1341+
? "compact"
1342+
: isMobileBreakpoint(width, height)
1343+
? "mobile"
1344+
: "full";
1345+
};
1346+
1347+
export const isMobileBreakpoint = (width: number, height: number) => {
1348+
return (
1349+
width <= MQ_MAX_MOBILE ||
1350+
(height < MQ_MAX_HEIGHT_LANDSCAPE && width < MQ_MAX_WIDTH_LANDSCAPE)
1351+
);
1352+
};
1353+
1354+
export const isTabletBreakpoint = (
1355+
editorWidth: number,
1356+
editorHeight: number,
1357+
) => {
1358+
const minSide = Math.min(editorWidth, editorHeight);
1359+
const maxSide = Math.max(editorWidth, editorHeight);
1360+
1361+
return minSide >= MQ_MIN_TABLET && maxSide <= MQ_MAX_TABLET;
1362+
};

packages/excalidraw/components/App.tsx

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,10 @@ import {
9999
MINIMUM_ARROW_SIZE,
100100
DOUBLE_TAP_POSITION_THRESHOLD,
101101
isMobileOrTablet,
102-
MQ_MAX_MOBILE,
103-
MQ_MIN_TABLET,
104-
MQ_MAX_TABLET,
105-
MQ_MAX_HEIGHT_LANDSCAPE,
106-
MQ_MAX_WIDTH_LANDSCAPE,
107102
isProdEnv,
103+
isMobileBreakpoint,
104+
isTabletBreakpoint,
105+
getUIMode,
108106
} from "@excalidraw/common";
109107

110108
import {
@@ -2452,20 +2450,6 @@ class App extends React.Component<AppProps, AppState> {
24522450
}
24532451
};
24542452

2455-
private isMobileBreakpoint = (width: number, height: number) => {
2456-
return (
2457-
width <= MQ_MAX_MOBILE ||
2458-
(height < MQ_MAX_HEIGHT_LANDSCAPE && width < MQ_MAX_WIDTH_LANDSCAPE)
2459-
);
2460-
};
2461-
2462-
private isTabletBreakpoint = (editorWidth: number, editorHeight: number) => {
2463-
const minSide = Math.min(editorWidth, editorHeight);
2464-
const maxSide = Math.max(editorWidth, editorHeight);
2465-
2466-
return minSide >= MQ_MIN_TABLET && maxSide <= MQ_MAX_TABLET;
2467-
};
2468-
24692453
private refreshViewportBreakpoints = () => {
24702454
const container = this.excalidrawContainerRef.current;
24712455
if (!container) {
@@ -2479,7 +2463,7 @@ class App extends React.Component<AppProps, AppState> {
24792463

24802464
const nextViewportState = updateObject(prevViewportState, {
24812465
isLandscape: editorWidth > editorHeight,
2482-
isMobile: this.isMobileBreakpoint(editorWidth, editorHeight),
2466+
isMobile: isMobileBreakpoint(editorWidth, editorHeight),
24832467
});
24842468

24852469
if (prevViewportState !== nextViewportState) {
@@ -2505,28 +2489,22 @@ class App extends React.Component<AppProps, AppState> {
25052489

25062490
const prevEditorState = this.device.editor;
25072491

2492+
const uiMode =
2493+
this.props.getUIMode?.({ width: editorWidth, height: editorHeight }) ??
2494+
getUIMode({ width: editorWidth, height: editorHeight });
2495+
25082496
const nextEditorState = updateObject(prevEditorState, {
2509-
isMobile: this.isMobileBreakpoint(editorWidth, editorHeight),
2497+
isMobile: uiMode === "mobile",
25102498
canFitSidebar: editorWidth > sidebarBreakpoint,
25112499
});
25122500

2513-
const stylesPanelMode =
2514-
// NOTE: we could also remove the isMobileOrTablet check here and
2515-
// always switch to compact mode when the editor is narrow (e.g. < MQ_MIN_WIDTH_DESKTOP)
2516-
// but not too narrow (> MQ_MAX_WIDTH_MOBILE)
2517-
this.isTabletBreakpoint(editorWidth, editorHeight) && isMobileOrTablet()
2518-
? "compact"
2519-
: this.isMobileBreakpoint(editorWidth, editorHeight)
2520-
? "mobile"
2521-
: "full";
2522-
25232501
// also check if we need to update the app state
25242502
this.setState((prevState) => ({
2525-
stylesPanelMode,
2503+
stylesPanelMode: uiMode,
25262504
// reset to box selection mode if the UI changes to full
25272505
// where you'd not be able to change the mode yourself currently
25282506
preferredSelectionTool:
2529-
stylesPanelMode === "full"
2507+
uiMode === "full"
25302508
? {
25312509
type: "selection",
25322510
initialized: true,

packages/excalidraw/components/LayerUI.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ const LayerUI = ({
232232
<div style={{ position: "relative" }}>
233233
{/* wrapping to Fragment stops React from occasionally complaining
234234
about identical Keys */}
235-
<tunnels.MainMenuTunnel.Out />
235+
<div className="excalidraw-ui-top-left">
236+
{renderTopLeftUI?.(false, appState)}
237+
<tunnels.MainMenuTunnel.Out />
238+
</div>
236239
{renderWelcomeScreen && <tunnels.WelcomeScreenMenuHintTunnel.Out />}
237240
</div>
238241
);

packages/excalidraw/components/MobileMenu.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ export const MobileMenu = ({
6565
DefaultSidebarTriggerTunnel,
6666
} = useTunnels();
6767
const renderAppTopBar = () => {
68-
const topRightUI = renderTopRightUI?.(true, appState) ?? (
69-
<DefaultSidebarTriggerTunnel.Out />
68+
const topRightUI = (
69+
<div className="excalidraw-ui-top-right">
70+
{renderTopRightUI?.(true, appState)}
71+
<DefaultSidebarTriggerTunnel.Out />
72+
</div>
7073
);
7174

7275
const topLeftUI = (

packages/excalidraw/css/styles.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ body.excalidraw-cursor-resize * {
321321
gap: 0.5rem;
322322
}
323323

324+
.excalidraw-ui-top-right {
325+
display: flex;
326+
align-items: center;
327+
gap: 0.5rem;
328+
}
329+
324330
.App-toolbar-content {
325331
display: flex;
326332
flex-direction: column;

packages/excalidraw/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
6161
ui,
6262
interactive,
6363
activeTool,
64+
getUIMode,
6465
} = props;
6566

6667
const canvasActions = props.UIOptions?.canvasActions;
@@ -159,6 +160,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
159160
ui={ui}
160161
interactive={interactive}
161162
activeTool={activeTool}
163+
getUIMode={getUIMode}
162164
>
163165
{children}
164166
</App>
@@ -336,3 +338,9 @@ export { duplicateElements, duplicateElement } from "../element/src/duplicate";
336338
export { parseMermaidToExcalidraw } from "@excalidraw/mermaid-to-excalidraw";
337339

338340
export { CommandPalette } from "./components/CommandPalette/CommandPalette";
341+
342+
export {
343+
getUIMode,
344+
isMobileBreakpoint,
345+
isTabletBreakpoint,
346+
} from "@excalidraw/common";

packages/excalidraw/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ export interface ExcalidrawProps {
648648
type: "custom";
649649
customType: string;
650650
};
651+
getUIMode?: (editorDimensions: {
652+
width: number;
653+
height: number;
654+
}) => "mobile" | "compact" | "full";
651655
}
652656

653657
export type SceneData = {

0 commit comments

Comments
 (0)