|
1 |
| -import React from 'react' |
| 1 | +import React, {type CSSProperties, type PropsWithChildren} from 'react' |
2 | 2 | import {clsx} from 'clsx'
|
3 | 3 | import styled, {createGlobalStyle} from 'styled-components'
|
4 |
| -import type {ComponentProps} from './utils/types' |
5 | 4 | import type {SystemCommonProps, SystemTypographyProps} from './constants'
|
6 | 5 | import {COMMON, TYPOGRAPHY} from './constants'
|
7 | 6 | import {useTheme} from './ThemeProvider'
|
8 | 7 | import {useFeatureFlag} from './FeatureFlags'
|
9 |
| -import {toggleStyledComponent} from './internal/utils/toggleStyledComponent' |
| 8 | +import Box from './Box' |
| 9 | +import type {SxProp} from './sx' |
| 10 | +import {includesSystemProps, getTypographyAndCommonProps} from './utils/includeSystemProps' |
| 11 | + |
10 | 12 | import classes from './BaseStyles.module.css'
|
11 | 13 |
|
12 | 14 | // load polyfill for :focus-visible
|
@@ -35,45 +37,108 @@ const GlobalStyle = createGlobalStyle<{colorScheme?: 'light' | 'dark'}>`
|
35 | 37 | }
|
36 | 38 | `
|
37 | 39 |
|
38 |
| -const Base = toggleStyledComponent( |
39 |
| - CSS_MODULES_FEATURE_FLAG, |
40 |
| - 'div', |
41 |
| - styled.div<SystemTypographyProps & SystemCommonProps>` |
42 |
| - ${TYPOGRAPHY}; |
43 |
| - ${COMMON}; |
44 |
| - `, |
45 |
| -) |
| 40 | +const StyledDiv = styled.div<SystemTypographyProps & SystemCommonProps>` |
| 41 | + ${TYPOGRAPHY}; |
| 42 | + ${COMMON}; |
| 43 | +` |
46 | 44 |
|
47 |
| -export type BaseStylesProps = ComponentProps<typeof Base> |
| 45 | +export type BaseStylesProps = PropsWithChildren & { |
| 46 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 47 | + as?: React.ComponentType<any> | keyof JSX.IntrinsicElements |
| 48 | + className?: string |
| 49 | + style?: CSSProperties |
| 50 | + color?: string // Fixes `color` ts-error |
| 51 | +} & SystemTypographyProps & |
| 52 | + SystemCommonProps & |
| 53 | + SxProp |
48 | 54 |
|
49 | 55 | function BaseStyles(props: BaseStylesProps) {
|
50 |
| - const {children, color = 'fg.default', fontFamily = 'normal', lineHeight = 'default', className, ...rest} = props |
| 56 | + const { |
| 57 | + children, |
| 58 | + color = 'var(--fgColor-default)', |
| 59 | + fontFamily = 'normal', |
| 60 | + lineHeight = 'default', |
| 61 | + className, |
| 62 | + as: Component = 'div', |
| 63 | + ...rest |
| 64 | + } = props |
51 | 65 |
|
52 | 66 | const {colorScheme, dayScheme, nightScheme} = useTheme()
|
53 | 67 | const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)
|
54 | 68 |
|
55 |
| - const stylingProps = enabled ? {className: clsx(classes.BaseStyles, className)} : {className} |
| 69 | + if (enabled) { |
| 70 | + const newClassName = clsx(classes.BaseStyles, className) |
| 71 | + |
| 72 | + // If props includes TYPOGRAPHY or COMMON props, pass them to the Box component |
| 73 | + if (includesSystemProps(props)) { |
| 74 | + const systemProps = getTypographyAndCommonProps(props) |
| 75 | + return ( |
| 76 | + // @ts-ignore shh |
| 77 | + <Box |
| 78 | + as={Component} |
| 79 | + className={newClassName} |
| 80 | + color={color} |
| 81 | + fontFamily={fontFamily} |
| 82 | + lineHeight={lineHeight} |
| 83 | + data-portal-root |
| 84 | + /** |
| 85 | + * We need to map valid primer/react color modes onto valid color modes for primer/primitives |
| 86 | + * valid color modes for primer/primitives: auto | light | dark |
| 87 | + * valid color modes for primer/primer: auto | day | night | light | dark |
| 88 | + */ |
| 89 | + data-color-mode={colorScheme?.includes('dark') ? 'dark' : 'light'} |
| 90 | + data-light-theme={dayScheme} |
| 91 | + data-dark-theme={nightScheme} |
| 92 | + style={systemProps} |
| 93 | + {...rest} |
| 94 | + > |
| 95 | + {children} |
| 96 | + </Box> |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + return ( |
| 101 | + <Component |
| 102 | + className={newClassName} |
| 103 | + color={color} |
| 104 | + fontFamily={fontFamily} |
| 105 | + lineHeight={lineHeight} |
| 106 | + data-portal-root |
| 107 | + /** |
| 108 | + * We need to map valid primer/react color modes onto valid color modes for primer/primitives |
| 109 | + * valid color modes for primer/primitives: auto | light | dark |
| 110 | + * valid color modes for primer/primer: auto | day | night | light | dark |
| 111 | + */ |
| 112 | + data-color-mode={colorScheme?.includes('dark') ? 'dark' : 'light'} |
| 113 | + data-light-theme={dayScheme} |
| 114 | + data-dark-theme={nightScheme} |
| 115 | + {...rest} |
| 116 | + > |
| 117 | + {children} |
| 118 | + </Component> |
| 119 | + ) |
| 120 | + } |
56 | 121 |
|
57 |
| - /** |
58 |
| - * We need to map valid primer/react color modes onto valid color modes for primer/primitives |
59 |
| - * valid color modes for primer/primitives: auto | light | dark |
60 |
| - * valid color modes for primer/primer: auto | day | night | light | dark |
61 |
| - */ |
62 | 122 | return (
|
63 |
| - <Base |
64 |
| - {...rest} |
65 |
| - {...stylingProps} |
| 123 | + <StyledDiv |
| 124 | + className={className} |
66 | 125 | color={color}
|
67 | 126 | fontFamily={fontFamily}
|
68 | 127 | lineHeight={lineHeight}
|
69 | 128 | data-portal-root
|
| 129 | + /** |
| 130 | + * We need to map valid primer/react color modes onto valid color modes for primer/primitives |
| 131 | + * valid color modes for primer/primitives: auto | light | dark |
| 132 | + * valid color modes for primer/primer: auto | day | night | light | dark |
| 133 | + */ |
70 | 134 | data-color-mode={colorScheme?.includes('dark') ? 'dark' : 'light'}
|
71 | 135 | data-light-theme={dayScheme}
|
72 | 136 | data-dark-theme={nightScheme}
|
| 137 | + {...rest} |
73 | 138 | >
|
74 |
| - {!enabled && <GlobalStyle colorScheme={colorScheme?.includes('dark') ? 'dark' : 'light'} />} |
| 139 | + <GlobalStyle colorScheme={colorScheme?.includes('dark') ? 'dark' : 'light'} /> |
75 | 140 | {children}
|
76 |
| - </Base> |
| 141 | + </StyledDiv> |
77 | 142 | )
|
78 | 143 | }
|
79 | 144 |
|
|
0 commit comments