Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/media merge with variants #3

Merged
merged 2 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 55 additions & 38 deletions example/src/Example.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,72 @@
import { Switch } from 'react-native';
import { StatusBar } from 'expo-status-bar';

import { Stack, Text, useColorMode, Media } from './components';
import { useState } from 'react';
import { Stack, Text, useColorMode, Media, Heading } from './components';
import { styled } from './styles';

export default function Example() {
const { toggleColorMode, colorMode } = useColorMode();
const [example, changeExample] = useState(false);

return (
<Wrapper>
<Content>
<Stack axis="y" space="4">
<Text variant="title1">Example app</Text>

<Stack axis="y" space="2">
{Array.from({ length: 5 }).map((_, i) => (
<Box key={i}>
<Stack axis="x" space="4">
<Text variant="body" color="primaryText">
Box {i + 1}
</Text>
<Text variant="body" color="primaryText">
XXX
</Text>
<Text variant="body" color="primaryText">
YYY
</Text>
</Stack>
</Box>
))}
<Stack axis="x" space="3" align="center">
<Text variant="title1">Example app</Text>
<Text variant="body">Switch Example</Text>
<Switch value={example} onValueChange={changeExample} />
</Stack>
{example && (
<Stack axis="y" space="2">
<Heading>Heading</Heading>
<Heading heading="h2">Heading</Heading>
<Heading heading="h3">Heading</Heading>
<Heading heading="h4">Heading</Heading>
<Heading heading="h5">Heading</Heading>
</Stack>
)}
{!example && (
<>
<Stack axis="y" space="2">
{Array.from({ length: 5 }).map((_, i) => (
<Box key={i}>
<Stack axis="x" space="4">
<Text variant="body" color="primaryText">
Box {i + 1}
</Text>
<Text variant="body" color="primaryText">
XXX
</Text>
<Text variant="body" color="primaryText">
YYY
</Text>
</Stack>
</Box>
))}
</Stack>

<Media
color={{
'@xxl': 'primary',
'@xl': 'secondary',
'@lg': 'third',
'@md': 'forth',
'@sm': 'fifth',
}}
>
Font size and color should change as viewport changes
</Media>
<Media
color={{
'@xxl': 'primary',
'@xl': 'secondary',
'@lg': 'third',
'@md': 'forth',
'@sm': 'fifth',
}}
>
Font size and color should change as viewport changes
</Media>

<Stack axis="x" space="3" align="center" justify="end">
<Text variant="body">Toggle color mode</Text>
<Switch
value={colorMode === 'dark'}
onValueChange={toggleColorMode}
/>
</Stack>
<Stack axis="x" space="3" align="center" justify="end">
<Text variant="body">Toggle color mode</Text>
<Switch
value={colorMode === 'dark'}
onValueChange={toggleColorMode}
/>
</Stack>
</>
)}
</Stack>
</Content>

Expand Down
32 changes: 32 additions & 0 deletions example/src/components/Heading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { TextProps as RNTextProps } from 'react-native';
import { styled } from '../styles';

export const Typography = styled('Text', {
color: '$plainText',
fontSizeRem: 1,
});

type HeadingSize = 'h1' | 'h2' | 'h3' | 'h4' | 'h5';

export type HeadingProps = RNTextProps & {
heading?: HeadingSize;
};

export const Heading = styled('Text', {
fontWeight: 'bold',
color: '$plainText',
variants: {
heading: {
h5: { fontSizeRem: 1.0, color: 'black' },
h4: { fontSizeRem: 1.1, color: 'red' },
h3: { fontSizeRem: 1.25, color: 'blue' },
h2: { fontSizeRem: 1.5, color: 'green' },
h1: { fontSizeRem: 2.5, color: 'purple' },
},
},
defaultVariants: {
heading: 'h1',
},
}).attrs(() => ({
accessibilityRole: 'text',
}));
1 change: 1 addition & 0 deletions example/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { Text } from './Text';
export { Media } from './Media';
export { Heading } from './Heading';
export { Stack } from './Stack';
export { Spacer } from './Spacer';
export { ColorModeProvider, useColorMode } from './ColorMode';
58 changes: 27 additions & 31 deletions src/internals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,7 @@ export function createStitches(config = {}) {
..._styles
} = styleObject;

let styles = {};

// Handle responsive base styles
if (typeof config.media === 'object') {
Object.entries(_styles).forEach(([key, val]) => {
if (key in config.media) {
// TODO: do we want to handle media range queries in the styled definition?
if (config.media[key] === true && typeof val === 'object') {
styles = { ...styles, ...val };
} else {
// Make sure other media styles are removed so they won't be passed on to StyleSheet
delete _styles[key];
}
} else {
styles[key] = val;
}
});
} else {
styles = _styles;
}
const styles = _styles;

const styleSheets = utils.createStyleSheets({
styles,
Expand All @@ -129,19 +110,23 @@ export function createStitches(config = {}) {
let variantStyles = [];
let compoundVariantStyles = [];

const mediaKey = useMemo(() => {
const { mediaKey, breakpoint } = useMemo(() => {
if (typeof config.media === 'object') {
const correctedWindowWidth =
PixelRatio.getPixelSizeForLayoutSize(windowWidth); // eslint-disable-line
PixelRatio.getPixelSizeForLayoutSize(windowWidth);

// TODO: how do we quarantee the order of breakpoint matches?
// The order of the media key value pairs should be constant
// but is that guaranteed? So if the keys are ordered from
// smallest screen size to largest everything should work ok...
return utils.resolveMediaRangeQuery(
const _mediaKey = utils.resolveMediaRangeQuery(
config.media,
correctedWindowWidth
);
return {
mediaKey: _mediaKey,
breakpoint: _mediaKey && `@${_mediaKey}`,
};
}
return undefined;
}, [windowWidth]);
Expand All @@ -155,7 +140,7 @@ export function createStitches(config = {}) {
propValue = defaultVariants[prop];
}

let styleSheetKey = '';
let styleSheetKey = `${prop}_${propValue}`;

// Handle responsive prop value
// NOTE: only one media query will be applied since the `styleSheetKey`
Expand All @@ -170,8 +155,7 @@ export function createStitches(config = {}) {
styleSheetKey = `${prop}_${propValue['@initial']}`;
}

if (mediaKey) {
const breakpoint = `@${mediaKey}`;
if (breakpoint) {
if (propValue[breakpoint] === undefined) return;
const val = config.media[mediaKey];
if (val === true) {
Expand All @@ -180,11 +164,18 @@ export function createStitches(config = {}) {
styleSheetKey = `${prop}_${propValue[breakpoint]}`;
}
}
} else {
styleSheetKey = `${prop}_${propValue}`;
}

return styleSheetKey ? styleSheet[styleSheetKey] : undefined;
const extractedStyle = styleSheetKey
? styleSheet[styleSheetKey]
: undefined;

if (extractedStyle && breakpoint in extractedStyle) {
// WARNING: lodash merge modify first argument reference or skip if freezed object.
return merge({}, extractedStyle, extractedStyle[breakpoint]);
}

return extractedStyle;
})
.filter(Boolean);
}
Expand All @@ -200,7 +191,12 @@ export function createStitches(config = {}) {
compoundEntries.every(([prop, value]) => props[prop] === value)
) {
const key = utils.getCompoundKey(compoundEntries);
return styleSheet[key];
const extractedStyle = styleSheet[key];
if (extractedStyle && breakpoint in extractedStyle) {
// WARNING: lodash merge modify first argument reference or skip if freezed object.
return merge({}, extractedStyle, extractedStyle[breakpoint]);
}
return extractedStyle;
}
})
.filter(Boolean);
Expand All @@ -214,7 +210,7 @@ export function createStitches(config = {}) {
})
: {};

const mediaStyle = styleSheet.base[`@${mediaKey}`] || {};
const mediaStyle = styleSheet.base[breakpoint] || {};

const stitchesStyles = [
styleSheet.base,
Expand Down