Last active
May 19, 2021 09:02
-
-
Save crisu83/551ceb406a18ae5fdfc6ae3849dae796 to your computer and use it in GitHub Desktop.
Design system written in TypeScript and built on top of Style System (and Styled Components).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @typescript-eslint/no-explicit-any */ | |
// Utility components on top of styled-system. | |
// Inspired by [Rebass](https://rebassjs.org/) and adopted to our needs. | |
// See: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase | |
import css, {SystemStyleObject} from '@styled-system/css'; | |
import shouldForwardProp from '@styled-system/should-forward-prop'; | |
import { | |
ComponentPropsWithRef, | |
ComponentPropsWithoutRef, | |
ElementType, | |
FunctionComponent, | |
} from 'react'; | |
import { | |
ThemedStyledInterface, | |
ThemedStyledProps, | |
default as baseStyled, | |
useTheme as baseUseTheme, | |
} from 'styled-components'; | |
import { | |
BorderProps, | |
ColorProps, | |
FlexboxProps, | |
GridProps as StyledSystemGridProps, | |
LayoutProps, | |
ShadowProps, | |
SpaceProps, | |
TypographyProps, | |
border, | |
color, | |
compose, | |
flexbox, | |
grid, | |
layout, | |
shadow, | |
space, | |
typography, | |
} from 'styled-system'; | |
import {Theme} from '@/design-system'; | |
export const styled: ThemedStyledInterface<Theme> = baseStyled; | |
export const useTheme = () => baseUseTheme() as Theme; | |
export type StyledProps<P = {}> = ThemedStyledProps<P, Theme>; | |
type SxProp = SystemStyleObject; | |
type BaseProps = { | |
as?: string; | |
ref?: any; | |
sx?: SxProp; | |
variant?: string; | |
}; | |
const sx = (props: {sx: SxProp; theme: Theme}) => | |
css(props.sx)(props.theme as any); | |
export type BoxProps = BaseProps & | |
BorderProps & | |
ColorProps & | |
LayoutProps & | |
ShadowProps & | |
SpaceProps & | |
TypographyProps; | |
export type BoxPropsWithoutRef<T extends ElementType<any>> = BoxProps & | |
ComponentPropsWithoutRef<T>; | |
export type BoxPropsWithRef<T extends ElementType<any>> = BoxProps & | |
ComponentPropsWithRef<T>; | |
export const Box = styled('div').withConfig({ | |
shouldForwardProp: shouldForwardProp as any, | |
})( | |
{ | |
boxSizing: 'border-box', | |
margin: 0, | |
minWidth: 0, | |
}, | |
sx, | |
compose(border, color, layout, shadow, space, typography) | |
) as FunctionComponent<BoxPropsWithoutRef<'div'>>; | |
export type FlexProps = BaseProps & BoxPropsWithoutRef<'div'> & FlexboxProps; | |
export const Flex = styled(Box)( | |
{ | |
display: 'flex', | |
}, | |
flexbox | |
) as FunctionComponent<FlexProps>; | |
export const FlexColumn = styled(Flex)({ | |
flexDirection: 'column', | |
}) as FunctionComponent<FlexProps>; | |
export const FlexRow = styled(Flex)({ | |
flexDirection: 'row', | |
}) as FunctionComponent<FlexProps>; | |
export type GridProps = BaseProps & BoxProps & StyledSystemGridProps; | |
export const Grid = styled(Box)( | |
{ | |
display: 'grid', | |
}, | |
grid | |
) as FunctionComponent<GridProps>; | |
export type TextProps = BaseProps & | |
BorderProps & | |
ColorProps & | |
LayoutProps & | |
ShadowProps & | |
SpaceProps & | |
TypographyProps; | |
export type TextPropsWithoutRef<T extends ElementType<any>> = | |
ComponentPropsWithoutRef<T> & TextProps; | |
export type TextPropsWithRef<T extends ElementType<any>> = | |
ComponentPropsWithRef<T> & TextProps; | |
export const Text = styled('span').withConfig({ | |
shouldForwardProp: shouldForwardProp as any, | |
})( | |
sx, | |
compose(border, color, layout, space, shadow, typography) | |
) as FunctionComponent<TextPropsWithoutRef<'span'>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export * from './core'; | |
export {Button} from './Button'; | |
export type {ButtonProps} from './Button'; | |
// More components ... | |
export {theme} from './theme'; | |
export type {Theme} from './theme'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export enum ThemeColor { | |
Error = '#f00', | |
Success = '#0f0', | |
Warning = '#ff0', | |
} | |
export const theme = { | |
borderWidths: { | |
large: 8, | |
medium: 5, | |
small: 3, | |
thin: 1, | |
}, | |
colors: { | |
error: '#f00', | |
success: '#0f0', | |
warning: '#ff0', | |
}, | |
fontWeights: { | |
bold: 700, | |
light: 300, | |
normal: 400, | |
}, | |
fonts: { | |
body: "'Open Sans', sans-serif", | |
}, | |
lineHeights: { | |
body: 1.5, | |
}, | |
radii: { | |
large: 20, | |
medium: 10, | |
none: 0, | |
round: '100%', | |
small: 5, | |
}, | |
shadows: { | |
large: '0 0 30px rgba(0, 0, 0, 0.1)', | |
medium: '0 0 20px rgba(0, 0, 0, 0.1)', | |
small: '0 0 10px rgba(0, 0, 0, 0.1)', | |
}, | |
sizes: { | |
full: '100%', | |
}, | |
}; | |
export type Theme = typeof theme; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment