Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhabib committed Sep 13, 2024
1 parent d29cc80 commit cc2cb89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Playroom/Playroom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as styles from './Playroom.css';
import { Box } from './Box/Box';

import { assignInlineVars } from '@vanilla-extract/dynamic';
import { editorSize as editorSize } from './Playroom.css';
import { editorSize } from './Playroom.css';

const resizableConfig = (position: EditorPosition = 'bottom') => ({
top: position === 'bottom',
Expand Down Expand Up @@ -135,7 +135,7 @@ export default ({ components, themes, widths, snippets }: PlayroomProps) => {
const isVerticalEditor = editorPosition === 'right';
const isHorizontalEditor = editorPosition === 'bottom';
const sizeStyles = {
height: isHorizontalEditor ? editorHeight : 'auto', // issue in ff & safari when not a string
height: isHorizontalEditor ? editorHeight : 'auto',
width: isVerticalEditor ? editorWidth : 'auto',
};
const editorContainer =
Expand Down
45 changes: 29 additions & 16 deletions src/StoreContext/StoreContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ const applyColorScheme = (colorScheme: Exclude<ColorScheme, 'system'>) => {
]('data-playroom-dark', '');
};

function getSizeAsPercentage(
mode: 'height' | 'width',
size: string | undefined
): string | undefined {
if (!size || !size.endsWith('px')) {
return;
}

const storedSizeAsNumber = parseInt(size.split('px')[0], 10);

return convertAndStoreSizeAsPercentage(mode, storedSizeAsNumber);
}

function convertAndStoreSizeAsPercentage(
mode: 'height' | 'width',
size: number
Expand Down Expand Up @@ -145,9 +158,17 @@ const createReducer =
(state: State, action: Action): State => {
switch (action.type) {
case 'initialLoad': {
const { editorHeight, editorWidth } = action.payload;

// If the stored size is in pixels, convert it to a percentage
const updatedEditorWidth = getSizeAsPercentage('width', editorWidth);
const updatedEditorHeight = getSizeAsPercentage('height', editorHeight);

return {
...state,
...action.payload,
editorWidth: updatedEditorWidth || state.editorWidth,
editorHeight: updatedEditorHeight || state.editorHeight,
};
}

Expand Down Expand Up @@ -497,8 +518,8 @@ export const StoreProvider = ({
Promise.all([
store.getItem<string>('code'),
store.getItem<EditorPosition>('editorPosition'),
store.getItem<number | string>('editorHeight'), // Number type deprecated
store.getItem<number | string>('editorWidth'), // Number type deprecated
store.getItem<string | number>('editorHeight'), // Number type deprecated
store.getItem<string | number>('editorWidth'), // Number type deprecated
store.getItem<number[]>('visibleWidths'),
store.getItem<string[]>('visibleThemes'),
store.getItem<ColorScheme>('colorScheme'),
Expand All @@ -516,14 +537,14 @@ export const StoreProvider = ({
const editorPosition = storedPosition;

const editorHeight =
(typeof storedHeight === 'string' ? storedHeight : null) ||
(typeof storedHeight === 'number' ? `${storedHeight}px` : null) ||
defaultEditorSize;
(typeof storedHeight === 'number'
? `${storedHeight}px`
: storedHeight) || defaultEditorSize;

const editorWidth =
(typeof storedWidth === 'string' ? storedWidth : null) ||
(typeof storedWidth === 'number' ? `${storedWidth}px` : null) ||
defaultEditorSize;
(typeof storedWidth === 'number'
? `${storedWidth}px`
: storedWidth) || defaultEditorSize;

const visibleWidths =
widthsFromQuery ||
Expand Down Expand Up @@ -552,14 +573,6 @@ export const StoreProvider = ({
ready: true,
},
});

// Converted deprecated pixel size preferences to percentage
if (typeof storedHeight === 'number') {
convertAndStoreSizeAsPercentage('height', storedHeight);
}
if (typeof storedWidth === 'number') {
convertAndStoreSizeAsPercentage('width', storedWidth);
}
}
);
}, [hasThemesConfigured]);
Expand Down

0 comments on commit cc2cb89

Please sign in to comment.