-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
styles.ts
381 lines (316 loc) · 9.67 KB
/
styles.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* External dependencies
*/
import * as Ariakit from '@ariakit/react';
import { css, keyframes } from '@emotion/react';
import styled from '@emotion/styled';
/**
* Internal dependencies
*/
import { COLORS, font, rtl, CONFIG } from '../utils';
import { space } from '../utils/space';
import Icon from '../icon';
import { Truncate } from '../truncate';
import type { DropdownMenuContext } from './types';
const ANIMATION_PARAMS = {
SCALE_AMOUNT_OUTER: 0.82,
SCALE_AMOUNT_CONTENT: 0.9,
DURATION: '400ms',
EASING: 'cubic-bezier(0.33, 0, 0, 1)',
};
const CONTENT_WRAPPER_PADDING = space( 1 );
const ITEM_PADDING_BLOCK = space( 2 );
const ITEM_PADDING_INLINE = space( 3 );
// TODO:
// - border color and divider color are different from COLORS.theme variables
// - lighter text color is not defined in COLORS.theme, should it be?
// - lighter background color is not defined in COLORS.theme, should it be?
const DEFAULT_BORDER_COLOR = COLORS.theme.gray[ 300 ];
const DIVIDER_COLOR = COLORS.theme.gray[ 200 ];
const LIGHTER_TEXT_COLOR = COLORS.theme.gray[ 700 ];
const LIGHT_BACKGROUND_COLOR = COLORS.theme.gray[ 100 ];
const TOOLBAR_VARIANT_BORDER_COLOR = COLORS.theme.foreground;
const DEFAULT_BOX_SHADOW = `0 0 0 ${ CONFIG.borderWidth } ${ DEFAULT_BORDER_COLOR }, ${ CONFIG.elevationXSmall }`;
const TOOLBAR_VARIANT_BOX_SHADOW = `0 0 0 ${ CONFIG.borderWidth } ${ TOOLBAR_VARIANT_BORDER_COLOR }`;
const GRID_TEMPLATE_COLS = 'minmax( 0, max-content ) 1fr';
const scaleYOuter = keyframes( {
from: {
transform: `scaleY(${ ANIMATION_PARAMS.SCALE_AMOUNT_OUTER })`,
},
} );
const scaleYContent = keyframes( {
// Cause the content to scale at a different rate than the outer container:
// - counter the outer scale factor by doing "1 / scaleAmountOuter"
// - then apply the content scale factor
from: {
transform: `scaleY(calc(1 / ${ ANIMATION_PARAMS.SCALE_AMOUNT_OUTER } * ${ ANIMATION_PARAMS.SCALE_AMOUNT_CONTENT }))`,
},
} );
const fadeIn = keyframes( {
from: {
opacity: 0,
},
} );
export const MenuPopoverOuterWrapper = styled.div<
Pick< DropdownMenuContext, 'variant' >
>`
position: relative;
background-color: ${ COLORS.ui.background };
border-radius: ${ CONFIG.radiusMedium };
${ ( props ) => css`
box-shadow: ${ props.variant === 'toolbar'
? TOOLBAR_VARIANT_BOX_SHADOW
: DEFAULT_BOX_SHADOW };
` }
overflow: hidden;
/* Animation */
@media not ( prefers-reduced-motion ) {
&:has( [data-open] ) {
animation-duration: ${ ANIMATION_PARAMS.DURATION };
animation-timing-function: ${ ANIMATION_PARAMS.EASING };
will-change: transform, opacity;
}
&:has( [data-open][data-side='bottom'] ) {
transform-origin: top;
}
&:has( [data-open][data-side='top'] ) {
transform-origin: bottom;
}
&:has( [data-open][data-side='bottom'] ),
&:has( [data-open][data-side='top'] ) {
animation-name: ${ fadeIn }, ${ scaleYOuter };
}
&:has( [data-open][data-side='left'] ),
&:has( [data-open][data-side='right'] ) {
animation-name: ${ fadeIn };
}
}
`;
export const MenuPopoverInnerWrapper = styled.div`
position: relative;
/* Same as popover component */
/* TODO: is there a way to read the sass variable? */
z-index: 1000000;
display: grid;
grid-template-columns: ${ GRID_TEMPLATE_COLS };
grid-template-rows: auto;
box-sizing: border-box;
min-width: 160px;
max-width: 320px;
max-height: var( --popover-available-height );
padding: ${ CONTENT_WRAPPER_PADDING };
overscroll-behavior: contain;
overflow: auto;
/* Only visible in Windows High Contrast mode */
outline: 2px solid transparent !important;
/* Animation */
@media not ( prefers-reduced-motion ) {
&[data-open] {
animation-duration: inherit;
animation-timing-function: inherit;
will-change: transform, opacity;
transform-origin: inherit;
}
&[data-open][data-side='bottom'],
&[data-open][data-side='top'] {
animation-name: ${ fadeIn }, ${ scaleYContent };
}
}
`;
const baseItem = css`
all: unset;
position: relative;
min-height: ${ space( 10 ) };
box-sizing: border-box;
/* Occupy the width of all grid columns (ie. full width) */
grid-column: 1 / -1;
display: grid;
grid-template-columns: ${ GRID_TEMPLATE_COLS };
align-items: center;
@supports ( grid-template-columns: subgrid ) {
/*
* Define a grid layout which inherits the same columns configuration
* from the parent layout (ie. subgrid). This allows the menu
* to synchronize the indentation of all its items.
*/
grid-template-columns: subgrid;
}
font-size: ${ font( 'default.fontSize' ) };
font-family: inherit;
font-weight: normal;
line-height: 20px;
color: ${ COLORS.theme.foreground };
border-radius: ${ CONFIG.radiusSmall };
padding-block: ${ ITEM_PADDING_BLOCK };
padding-inline: ${ ITEM_PADDING_INLINE };
/*
* Make sure that, when an item is scrolled into view (eg. while using the
* keyboard to move focus), the whole item comes into view
*/
scroll-margin: ${ CONTENT_WRAPPER_PADDING };
user-select: none;
outline: none;
&[aria-disabled='true'] {
color: ${ COLORS.ui.textDisabled };
cursor: not-allowed;
}
/* Active item (including hover)
* Note: we should be able to remove :focus-visible once
* https://github.com/ariakit/ariakit/issues/4083 is fixed and released
*/
&[data-active-item]:not( [data-focus-visible] ):not( :focus-visible ):not(
[aria-disabled='true']
) {
background-color: ${ COLORS.theme.accent };
color: ${ COLORS.white };
}
/* Keyboard focus (focus-visible)
* Note: we should be able to remove :focus-visible once
* https://github.com/ariakit/ariakit/issues/4083 is fixed and released
*/
&[data-focus-visible],
&:focus-visible {
box-shadow: 0 0 0 1.5px ${ COLORS.theme.accent };
/* Only visible in Windows High Contrast mode */
outline: 2px solid transparent;
}
/* Active (ie. pressed, mouse down) */
&:active,
&[data-active] {
/* TODO: should there be a visual active state? */
}
/* When the item is the trigger of an open submenu */
${ MenuPopoverInnerWrapper }:not(:focus) &:not(:focus)[aria-expanded="true"] {
background-color: ${ LIGHT_BACKGROUND_COLOR };
color: ${ COLORS.theme.foreground };
}
svg {
fill: currentColor;
}
`;
export const DropdownMenuItem = styled( Ariakit.MenuItem )`
${ baseItem };
`;
export const DropdownMenuCheckboxItem = styled( Ariakit.MenuItemCheckbox )`
${ baseItem };
`;
export const DropdownMenuRadioItem = styled( Ariakit.MenuItemRadio )`
${ baseItem };
`;
export const ItemPrefixWrapper = styled.span`
/* Always occupy the first column, even when auto-collapsing */
grid-column: 1;
/*
* Even when the item is not checked, occupy the same screen space to avoid
* the space collapside when no items are checked.
*/
${ DropdownMenuCheckboxItem } > &,
${ DropdownMenuRadioItem } > & {
/* Same width as the check icons */
min-width: ${ space( 6 ) };
}
${ DropdownMenuCheckboxItem } > &,
${ DropdownMenuRadioItem } > &,
&:not( :empty ) {
margin-inline-end: ${ space( 2 ) };
}
display: flex;
align-items: center;
justify-content: center;
color: ${ LIGHTER_TEXT_COLOR };
/*
* When the parent menu item is active, except when it's a non-focused/hovered
* submenu trigger (in that case, color should not be inherited)
*/
[data-active-item]:not( [data-focus-visible] ) > &,
/* When the parent menu item is disabled */
[aria-disabled='true'] > & {
color: inherit;
}
`;
export const DropdownMenuItemContentWrapper = styled.div`
/*
* Always occupy the second column, since the first column
* is taken by the prefix wrapper (when displayed).
*/
grid-column: 2;
display: flex;
align-items: center;
justify-content: space-between;
gap: ${ space( 3 ) };
pointer-events: none;
`;
export const DropdownMenuItemChildrenWrapper = styled.div`
flex: 1;
display: inline-flex;
flex-direction: column;
gap: ${ space( 1 ) };
`;
export const ItemSuffixWrapper = styled.span`
flex: 0 1 fit-content;
min-width: 0;
width: fit-content;
display: flex;
align-items: center;
justify-content: center;
gap: ${ space( 3 ) };
color: ${ LIGHTER_TEXT_COLOR };
/*
* When the parent menu item is active, except when it's a non-focused/hovered
* submenu trigger (in that case, color should not be inherited)
*/
[data-active-item]:not( [data-focus-visible] ) *:not(${ MenuPopoverInnerWrapper }) &,
/* When the parent menu item is disabled */
[aria-disabled='true'] *:not(${ MenuPopoverInnerWrapper }) & {
color: inherit;
}
`;
export const DropdownMenuGroup = styled( Ariakit.MenuGroup )`
/* Ignore this element when calculating the layout. Useful for subgrid */
display: contents;
`;
export const DropdownMenuSeparator = styled( Ariakit.MenuSeparator )<
Pick< DropdownMenuContext, 'variant' >
>`
/* Occupy the width of all grid columns (ie. full width) */
grid-column: 1 / -1;
border: none;
height: ${ CONFIG.borderWidth };
background-color: ${ ( props ) =>
props.variant === 'toolbar'
? TOOLBAR_VARIANT_BORDER_COLOR
: DIVIDER_COLOR };
/* Align with menu items' content */
margin-block: ${ space( 2 ) };
margin-inline: ${ ITEM_PADDING_INLINE };
/* Only visible in Windows High Contrast mode */
outline: 2px solid transparent;
`;
export const SubmenuChevronIcon = styled( Icon )`
width: ${ space( 1.5 ) };
${ rtl(
{
transform: `scaleX(1)`,
},
{
transform: `scaleX(-1)`,
}
) };
`;
export const DropdownMenuItemLabel = styled( Truncate )`
font-size: ${ font( 'default.fontSize' ) };
line-height: 20px;
color: inherit;
`;
export const DropdownMenuItemHelpText = styled( Truncate )`
font-size: ${ font( 'helpText.fontSize' ) };
line-height: 16px;
color: ${ LIGHTER_TEXT_COLOR };
word-break: break-all;
[data-active-item]:not( [data-focus-visible] )
*:not( ${ MenuPopoverInnerWrapper } )
&,
[aria-disabled='true'] *:not( ${ MenuPopoverInnerWrapper } ) & {
color: inherit;
}
`;