-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathBaseStyles.tsx
More file actions
145 lines (124 loc) · 4.2 KB
/
BaseStyles.tsx
File metadata and controls
145 lines (124 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import type React from 'react'
import {type CSSProperties, type PropsWithChildren, type JSX} from 'react'
import {clsx} from 'clsx'
// eslint-disable-next-line import/no-namespace
import type * as styledSystem from 'styled-system'
import {useTheme} from './ThemeProvider'
import 'focus-visible'
import {createGlobalStyle} from 'styled-components'
export interface SystemCommonProps
extends styledSystem.ColorProps,
styledSystem.SpaceProps,
styledSystem.DisplayProps {}
export interface SystemTypographyProps extends styledSystem.TypographyProps {
whiteSpace?: 'normal' | 'nowrap' | 'pre' | 'pre-wrap' | 'pre-line'
}
const GlobalStyle = createGlobalStyle<{colorScheme?: 'light' | 'dark'}>`
* {
box-sizing: border-box;
}
body {
margin: 0;
}
table {
/* stylelint-disable-next-line primer/borders */
border-collapse: collapse;
}
[data-color-mode='light'] input {
color-scheme: light;
}
[data-color-mode='dark'] input {
color-scheme: dark;
}
@media (prefers-color-scheme: light) {
[data-color-mode='auto'][data-light-theme*='light'] {
color-scheme: light;
}
}
@media (prefers-color-scheme: dark) {
[data-color-mode='auto'][data-dark-theme*='dark'] {
color-scheme: dark;
}
}
[role='button']:focus:not(:focus-visible):not(:global(.focus-visible)),
[role='tabpanel'][tabindex='0']:focus:not(:focus-visible):not(:global(.focus-visible)),
button:focus:not(:focus-visible):not(:global(.focus-visible)),
summary:focus:not(:focus-visible):not(:global(.focus-visible)),
a:focus:not(:focus-visible):not(:global(.focus-visible)) {
outline: none;
box-shadow: none;
}
[tabindex='0']:focus:not(:focus-visible):not(:global(.focus-visible)),
details-dialog:focus:not(:focus-visible):not(:global(.focus-visible)) {
outline: none;
}
/* -------------------------------------------------------------------------- */
.BaseStyles {
font-family: var(--BaseStyles-fontFamily, var(--fontStack-system));
/* stylelint-disable-next-line primer/typography */
line-height: var(--BaseStyles-lineHeight, 1.5);
/* stylelint-disable-next-line primer/colors */
color: var(--BaseStyles-fgColor, var(--fgColor-default));
/*
* PERFORMANCE: Removed :has([data-color-mode]) selectors that scanned entire DOM.
* Input color-scheme is already handled by global selectors above:
* [data-color-mode='light'] input { color-scheme: light; }
* [data-color-mode='dark'] input { color-scheme: dark; }
*/
/* Low-specificity default link styling */
:where(a:not([class*='prc-']):not([class*='PRC-']):not([class*='Primer_Brand__'])) {
color: var(--fgColor-accent, var(--color-accent-fg));
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
`
export type BaseStylesProps = PropsWithChildren & {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
as?: React.ComponentType<any> | keyof JSX.IntrinsicElements
className?: string
style?: CSSProperties
color?: string // Fixes `color` ts-error
} & SystemTypographyProps &
SystemCommonProps
export function BaseStyles({
children,
color,
fontFamily,
lineHeight,
className,
as: Component = 'div',
style,
...rest
}: BaseStylesProps) {
const {colorMode, colorScheme, dayScheme, nightScheme} = useTheme()
const baseStyles = {
['--BaseStyles-fgColor']: color,
['--BaseStyles-fontFamily']: fontFamily,
['--BaseStyles-lineHeight']: lineHeight,
}
return (
<Component
className={clsx('BaseStyles', className)}
data-portal-root
/**
* We need to map valid primer/react color modes onto valid color modes for primer/primitives
* valid color modes for primer/primitives: auto | light | dark
* valid color modes for primer/primer: auto | day | night | light | dark
*/
data-color-mode={colorMode === 'auto' ? 'auto' : colorScheme?.includes('dark') ? 'dark' : 'light'}
data-light-theme={dayScheme}
data-dark-theme={nightScheme}
style={{
...baseStyles,
...style,
}}
{...rest}
>
<GlobalStyle colorScheme={colorScheme?.includes('dark') ? 'dark' : 'light'} />
{children}
</Component>
)
}