import * as React from 'react'; import { DebugEngine, styled, StandardEngine, withStyle, withStyleDeep, withTransform, withWrapper, Provider, useStyletron, DevProvider, } from 'styletron-react'; // styled() // -------------------------- // Static Styles const BasicStyled = styled('div', { color: 'red' }); // Dynamic Styles interface DynamicStyledProps { $fraction: number; } const DynamicStyled = styled('div', (props: DynamicStyledProps) => { return { color: props.$fraction < 0.5 ? 'red' : 'green' }; }); ; const Button = () => ; const StyledButton = styled(Button, { color: 'blue' }); ; const DynamicStyledButton = styled(Button, (props: DynamicStyledProps) => { return { color: props.$fraction < 0.5 ? 'red' : 'green' }; }); ; const ComplexButton = ({ isDisabled }: { isDisabled: boolean }) => ( ); const StyledComplexButton = styled(ComplexButton, { color: 'blue' }); ; const DynamicStyledComplexButton = styled(ComplexButton, (props: DynamicStyledProps) => { return { color: props.$fraction < 0.5 ? 'red' : 'green' }; }); ; // Allows $as prop ; // Allows $style prop ; const $styleFn = (props: DynamicStyledProps) => ({ color: props.$fraction < 0.2 ? 'red' : 'green' }); ; // withStyle() // -------------------------- // Static Styles const WithStyledSimple = withStyle(BasicStyled, { color: 'blue' }); ; // Dynamic Styles interface WithStyledDynamicProps { $crushed: boolean; } const WithStyledDynamic = withStyle(BasicStyled, (props: WithStyledDynamicProps) => ({ letterSpacing: props.$crushed ? '-5px' : '0', })); ; // withStyleDeep() // -------------------------- // Static Styles const WithStyledDeepSimple = withStyleDeep(BasicStyled, { color: 'blue' }); ; // Dynamic Styles interface WithStyledDeepDynamicProps { $crushed: boolean; } const WithStyledDeepDynamic = withStyleDeep(BasicStyled, (props: WithStyledDeepDynamicProps) => ({ letterSpacing: props.$crushed ? '-5px' : '0', })); ; // withTransform() // -------------------------- interface WithTransformTestProps { $inline: boolean; } const WithTransformTest = withTransform(BasicStyled, (style, props: WithTransformTestProps) => { const display = style.display === 'none' ? 'none' : props.$inline ? 'inline-flex' : 'flex'; return { ...styled, display }; }); ; // withWrapper() // -------------------------- const PrettyButton = styled('button', { background: 'green' }); const { Consumer } = React.createContext(true); const WithWrapped = withWrapper(PrettyButton, Styled => props => ( {value => } )); ; // Style composition still works as normal; const StyledWithWrapper = withStyle(WithWrapped, { background: 'red' }); ; // // -------------------------- const engineNoop = (arg: any) => (arg ? '' : ''); const engine: StandardEngine = { renderStyle: engineNoop, renderKeyframes: engineNoop, renderFontFace: engineNoop, }; const App = () => ( ); ; // // -------------------------- const debug = new DebugEngine(); const DevApp = () => ( ); ; // useStyletron() // -------------------------- const [css] = useStyletron();
;