@registrucentras/rc-ses-react-components 2.0.0-rc.0 → 2.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +5 -1
  2. package/dist/assets/icons/CaretUpBoldIcon.d.ts +3 -0
  3. package/dist/assets/icons/CaretUpIcon.d.ts +3 -0
  4. package/dist/assets/icons/ListIcon.d.ts +3 -0
  5. package/dist/assets/icons/MinusIcon.d.ts +3 -0
  6. package/dist/assets/icons/NotePencilIcon.d.ts +3 -0
  7. package/dist/assets/icons/PlusIcon.d.ts +3 -0
  8. package/dist/assets/icons/TrashIcon.d.ts +3 -0
  9. package/dist/assets/icons/UserIcon.d.ts +3 -0
  10. package/dist/components/common/AdvancedList/components/AdvancedListItem/AdvancedListItem.types.d.ts +106 -0
  11. package/dist/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemLeading.d.ts +12 -0
  12. package/dist/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.d.ts +7 -0
  13. package/dist/components/common/AdvancedList/components/AdvancedListItem/hooks/usePrefersReducedMotion.d.ts +2 -0
  14. package/dist/components/common/AdvancedList/components/AdvancedListItem/index.d.ts +4 -0
  15. package/dist/components/common/AdvancedList/index.d.ts +11 -0
  16. package/dist/components/common/Button/index.d.ts +13 -3
  17. package/dist/components/common/SelectableCardList/components/SelectableCardListItem.d.ts +4 -1
  18. package/dist/components/common/SelectableCardList/index.d.ts +4 -1
  19. package/dist/components/common/Tooltip/index.d.ts +20 -0
  20. package/dist/library/icons.d.ts +9 -1
  21. package/dist/library/index.d.ts +5 -0
  22. package/dist/library/types.d.ts +2 -1
  23. package/dist/registrucentras-rc-ses-react-components.cjs +1 -1
  24. package/dist/registrucentras-rc-ses-react-components.js +4107 -3246
  25. package/dist/stories/components/actions/Button.stories.d.ts +4 -3
  26. package/dist/stories/components/display/AdvancedList.stories.d.ts +32 -0
  27. package/dist/stories/components/display/AdvancedListItem.stories.d.ts +33 -0
  28. package/dist/stories/components/display/SelectableCardList.stories.d.ts +8 -1
  29. package/dist/stories/components/display/Tooltip.stories.d.ts +48 -0
  30. package/dist/stories/components/inputs/DatePicker.stories.d.ts +23 -0
  31. package/dist/stories/components/loaders/LoadingSpinner.stories.d.ts +3 -3
  32. package/dist/stories/components/overlays/Dialog.stories.d.ts +6 -0
  33. package/dist/stories/components/overlays/Popover.stories.d.ts +12 -0
  34. package/dist/stories/theme/ThemedMuiComponents.stories.d.ts +25 -0
  35. package/dist/theme/light/MuiTooltip.d.ts +3 -0
  36. package/dist/theme/motion.d.ts +16 -0
  37. package/package.json +2 -1
  38. package/src/assets/icons/CaretUpBoldIcon.tsx +17 -0
  39. package/src/assets/icons/CaretUpIcon.tsx +9 -0
  40. package/src/assets/icons/ListIcon.tsx +17 -0
  41. package/src/assets/icons/MinusIcon.tsx +9 -0
  42. package/src/assets/icons/NotePencilIcon.tsx +9 -0
  43. package/src/assets/icons/PlusIcon.tsx +9 -0
  44. package/src/assets/icons/TrashIcon.tsx +23 -0
  45. package/src/assets/icons/UserIcon.tsx +17 -0
  46. package/src/components/common/AdvancedList/components/AdvancedListItem/AdvancedListItem.types.ts +100 -0
  47. package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemLeading.tsx +224 -0
  48. package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.tsx +263 -0
  49. package/src/components/common/AdvancedList/components/AdvancedListItem/hooks/usePrefersReducedMotion.test.ts +80 -0
  50. package/src/components/common/AdvancedList/components/AdvancedListItem/hooks/usePrefersReducedMotion.ts +28 -0
  51. package/src/components/common/AdvancedList/components/AdvancedListItem/index.test.tsx +631 -0
  52. package/src/components/common/AdvancedList/components/AdvancedListItem/index.tsx +332 -0
  53. package/src/components/common/AdvancedList/index.test.tsx +97 -0
  54. package/src/components/common/AdvancedList/index.tsx +52 -0
  55. package/src/components/common/Button/index.tsx +15 -1
  56. package/src/components/common/ListWithIcons/components/ListWithIconsItem.tsx +7 -5
  57. package/src/components/common/SelectableCardList/components/SelectableCardListItem.test.tsx +50 -0
  58. package/src/components/common/SelectableCardList/components/SelectableCardListItem.tsx +34 -0
  59. package/src/components/common/SelectableCardList/index.test.tsx +56 -2
  60. package/src/components/common/SelectableCardList/index.tsx +29 -10
  61. package/src/components/common/Tooltip/index.test.tsx +85 -0
  62. package/src/components/common/Tooltip/index.tsx +168 -0
  63. package/src/components/form/inputs/Select/index.test.tsx +102 -0
  64. package/src/components/form/inputs/Select/index.tsx +105 -52
  65. package/src/i18n/namespaces/common/en.json +14 -0
  66. package/src/i18n/namespaces/common/lt.json +14 -0
  67. package/src/library/icons.ts +16 -0
  68. package/src/library/index.ts +5 -0
  69. package/src/library/types.ts +2 -0
  70. package/src/setupTests.ts +7 -1
  71. package/src/stories/components/display/AdvancedList.stories.tsx +529 -0
  72. package/src/stories/components/display/AdvancedListItem.stories.tsx +653 -0
  73. package/src/stories/components/display/SelectableCardList.stories.tsx +19 -0
  74. package/src/stories/components/display/Tooltip.stories.tsx +67 -0
  75. package/src/stories/components/inputs/DatePicker.stories.tsx +36 -1
  76. package/src/stories/components/inputs/Select.stories.tsx +38 -2
  77. package/src/stories/components/overlays/Dialog.stories.tsx +34 -0
  78. package/src/stories/components/overlays/Popover.stories.tsx +65 -0
  79. package/src/stories/theme/ThemedMuiComponents.stories.tsx +177 -0
  80. package/src/theme/light/MuiAutocomplete.ts +3 -1
  81. package/src/theme/light/MuiInputBase.ts +4 -0
  82. package/src/theme/light/MuiTooltip.ts +40 -0
  83. package/src/theme/light/index.tsx +2 -0
  84. package/src/theme/motion.tsx +10 -0
@@ -0,0 +1,224 @@
1
+ import { Avatar, Box, IconButton, Radio, Stack, Typography } from '@mui/material'
2
+ import { RefObject } from 'react'
3
+ import { useTranslation } from 'react-i18next'
4
+
5
+ import CaretDownIcon from '@/assets/icons/CaretDownIcon'
6
+ import CaretUpIcon from '@/assets/icons/CaretUpIcon'
7
+ import ListIcon from '@/assets/icons/ListIcon'
8
+ import RcSesSimpleCheckbox from '@/components/form/inputs/SimpleCheckbox'
9
+ import palette from '@/theme/palette'
10
+
11
+ import { AdvancedListItemLeading as AdvancedListItemLeadingData } from '../AdvancedListItem.types'
12
+
13
+ type Props = {
14
+ leading: AdvancedListItemLeadingData
15
+ disabled?: boolean
16
+ inputRef?: RefObject<HTMLInputElement>
17
+ expandableId?: string
18
+ expanded?: boolean
19
+ titleId?: string
20
+ }
21
+
22
+ const stop = (e: { stopPropagation: () => void }) => e.stopPropagation()
23
+
24
+ const getAccessibleNameProps = (
25
+ explicitLabel: string | undefined,
26
+ titleId: string | undefined,
27
+ ) => {
28
+ if (explicitLabel) return { 'aria-label': explicitLabel }
29
+ if (titleId) return { 'aria-labelledby': titleId }
30
+ return {}
31
+ }
32
+
33
+ const AdvancedListItemLeading = ({
34
+ leading,
35
+ disabled,
36
+ inputRef,
37
+ expandableId,
38
+ expanded,
39
+ titleId,
40
+ }: Props) => {
41
+ const { t } = useTranslation('common', {
42
+ keyPrefix: 'components.AdvancedListItemLeading',
43
+ })
44
+
45
+ if (leading.type === 'reorder') {
46
+ return (
47
+ <Stack
48
+ onClick={stop}
49
+ sx={{
50
+ alignItems: 'center',
51
+ }}
52
+ >
53
+ {!!leading.onMoveUp && (
54
+ <IconButton
55
+ aria-label={t('moveUp')}
56
+ size='small'
57
+ disabled={disabled || leading.moveUpDisabled}
58
+ onClick={(e) => {
59
+ stop(e)
60
+ leading.onMoveUp?.()
61
+ }}
62
+ sx={{ padding: '0.125rem' }}
63
+ >
64
+ <CaretUpIcon size={16} weight='regular' fillColor={palette.primary[700]} />
65
+ </IconButton>
66
+ )}
67
+ {!!leading.onMoveDown && (
68
+ <IconButton
69
+ aria-label={t('moveDown')}
70
+ size='small'
71
+ disabled={disabled || leading.moveDownDisabled}
72
+ onClick={(e) => {
73
+ stop(e)
74
+ leading.onMoveDown?.()
75
+ }}
76
+ sx={{ padding: '0.125rem' }}
77
+ >
78
+ <CaretDownIcon size={16} fillColor={palette.primary[700]} />
79
+ </IconButton>
80
+ )}
81
+ </Stack>
82
+ )
83
+ }
84
+
85
+ if (leading.type === 'drag') {
86
+ return (
87
+ <Box
88
+ aria-label={leading['aria-label']}
89
+ aria-hidden={!leading['aria-label']}
90
+ sx={{
91
+ display: 'flex',
92
+ alignItems: 'center',
93
+ justifyContent: 'center',
94
+ width: '1.5rem',
95
+ height: '1.5rem',
96
+ cursor: disabled ? 'default' : 'grab',
97
+ }}
98
+ >
99
+ <ListIcon size={20} fillColor={palette.grey[500]} />
100
+ </Box>
101
+ )
102
+ }
103
+
104
+ if (leading.type === 'icon') {
105
+ const { icon: Icon } = leading
106
+
107
+ return (
108
+ <Box
109
+ aria-label={leading['aria-label']}
110
+ aria-hidden={!leading['aria-label']}
111
+ sx={{
112
+ display: 'flex',
113
+ alignItems: 'center',
114
+ justifyContent: 'center',
115
+ width: '1.5rem',
116
+ height: '1.5rem',
117
+ }}
118
+ >
119
+ <Icon size={24} fillColor={palette.grey[700]} />
120
+ </Box>
121
+ )
122
+ }
123
+
124
+ if (leading.type === 'avatar') {
125
+ return (
126
+ <Avatar
127
+ src={leading.src}
128
+ alt={leading.alt}
129
+ sx={{
130
+ display: 'flex',
131
+ width: '2rem',
132
+ height: '2rem',
133
+ justifyContent: 'center',
134
+ alignItems: 'center',
135
+ backgroundColor: palette.grey[100],
136
+ color: palette.grey[600],
137
+ fontSize: '0.8125rem',
138
+ fontWeight: 400,
139
+ lineHeight: '1.125rem',
140
+ }}
141
+ >
142
+ {leading.initials}
143
+ </Avatar>
144
+ )
145
+ }
146
+
147
+ if (leading.type === 'index') {
148
+ return (
149
+ <Box
150
+ sx={{
151
+ display: 'flex',
152
+ alignItems: 'center',
153
+ justifyContent: 'center',
154
+ width: '1.5rem',
155
+ height: '1.5rem',
156
+ }}
157
+ >
158
+ <Typography
159
+ variant='body2'
160
+ sx={{ fontSize: '0.9375rem', fontWeight: 400, lineHeight: '1.375rem' }}
161
+ color={palette.grey[600]}
162
+ >
163
+ {leading.index}
164
+ </Typography>
165
+ </Box>
166
+ )
167
+ }
168
+
169
+ if (leading.type === 'checkbox') {
170
+ return (
171
+ <RcSesSimpleCheckbox
172
+ checked={leading.checked}
173
+ disabled={disabled}
174
+ // MUI 9 replaced inputRef/inputProps with the `input` slot.
175
+ slotProps={{
176
+ input: {
177
+ ref: inputRef,
178
+ ...getAccessibleNameProps(leading['aria-label'], titleId),
179
+ ...(expandableId && {
180
+ 'aria-expanded': expanded,
181
+ 'aria-controls': expandableId,
182
+ }),
183
+ },
184
+ }}
185
+ onClick={stop}
186
+ onChange={(e) => {
187
+ stop(e)
188
+ leading.onChange(e.target.checked)
189
+ }}
190
+ sx={{ padding: 0 }}
191
+ />
192
+ )
193
+ }
194
+ // change to RcSesRadio when it is implemented
195
+ if (leading.type === 'radio') {
196
+ return (
197
+ <Radio
198
+ checked={leading.checked}
199
+ disabled={disabled}
200
+ name={leading.name}
201
+ slotProps={{
202
+ input: {
203
+ ref: inputRef,
204
+ ...getAccessibleNameProps(leading['aria-label'], titleId),
205
+ ...(expandableId && {
206
+ 'aria-expanded': expanded,
207
+ 'aria-controls': expandableId,
208
+ }),
209
+ },
210
+ }}
211
+ onClick={stop}
212
+ onChange={(e) => {
213
+ stop(e)
214
+ leading.onChange(e.target.checked)
215
+ }}
216
+ sx={{ padding: 0 }}
217
+ />
218
+ )
219
+ }
220
+
221
+ return null
222
+ }
223
+
224
+ export default AdvancedListItemLeading
@@ -0,0 +1,263 @@
1
+ import { Box, IconButton, Radio, Stack, Typography } from '@mui/material'
2
+ import { useTranslation } from 'react-i18next'
3
+
4
+ import CaretRightBoldIcon from '@/assets/icons/CaretRightBoldIcon'
5
+ import MinusRegularIcon from '@/assets/icons/MinusIcon'
6
+ import PlusRegularIcon from '@/assets/icons/PlusIcon'
7
+ import TrashIcon from '@/assets/icons/TrashIcon'
8
+ import RcSesBadge, { type RcSesBadgeProps } from '@/components/common/Badge'
9
+ import RcSesButton from '@/components/common/Button'
10
+ import RcSesSwitch from '@/components/common/Switch'
11
+ import RcSesSimpleCheckbox from '@/components/form/inputs/SimpleCheckbox'
12
+ import palette from '@/theme/palette'
13
+
14
+ import {
15
+ AdvancedListItemStatusColor,
16
+ AdvancedListItemTrailing as AdvancedListItemTrailingData,
17
+ } from '../AdvancedListItem.types'
18
+
19
+ type Props = {
20
+ trailing: AdvancedListItemTrailingData
21
+ disabled?: boolean
22
+ }
23
+
24
+ const NO_SHRINK_SX = { flexShrink: 0 } as const
25
+
26
+ const STEPPER_BUTTON_SX = {
27
+ padding: '0.5rem 0.25rem',
28
+ borderRadius: '0.375rem',
29
+ } as const
30
+
31
+ const STATUS_VARIANT_MAP: Record<
32
+ AdvancedListItemStatusColor,
33
+ RcSesBadgeProps['variant']
34
+ > = {
35
+ grey: 'neutral',
36
+ warning: 'warning',
37
+ success: 'success',
38
+ error: 'error',
39
+ }
40
+
41
+ const PRICE_BLOCK_SX = {
42
+ display: 'inline-flex',
43
+ flexDirection: 'column',
44
+ justifyContent: 'center',
45
+ alignItems: 'flex-end',
46
+ gap: '0.125rem',
47
+ } as const
48
+
49
+ const ACTION_BUTTON_SX = {
50
+ height: 'auto',
51
+ padding: '0.5rem 0.25rem',
52
+ fontWeight: 500,
53
+ lineHeight: '1.25rem',
54
+ color: palette.primary[700],
55
+ '&:not(.Mui-disabled) svg path': {
56
+ fill: palette.primary[700],
57
+ },
58
+ '.MuiButton-startIcon': {
59
+ marginRight: '0.375rem',
60
+ },
61
+ } as const
62
+
63
+ const stop = (e: React.SyntheticEvent) => e.stopPropagation()
64
+
65
+ const stopAnd = (fn: () => void) => (e: React.SyntheticEvent) => {
66
+ stop(e)
67
+ fn()
68
+ }
69
+
70
+ const withStop =
71
+ <E extends React.SyntheticEvent>(handler: (e: E) => void) =>
72
+ (e: E) => {
73
+ stop(e)
74
+ handler(e)
75
+ }
76
+
77
+ const AdvancedListItemTrailing = ({ trailing, disabled }: Props) => {
78
+ const { t } = useTranslation('common', {
79
+ keyPrefix: 'components.AdvancedListItemTrailing',
80
+ })
81
+
82
+ if (trailing.type === 'actions') {
83
+ return (
84
+ <Stack
85
+ direction='row'
86
+ onClick={stop}
87
+ sx={[
88
+ {
89
+ gap: '0.5rem',
90
+ },
91
+ ...(Array.isArray(NO_SHRINK_SX) ? NO_SHRINK_SX : [NO_SHRINK_SX]),
92
+ ]}
93
+ >
94
+ {trailing.actions.map(
95
+ ({ label, icon: Icon, disabled: actionDisabled, onClick }) => (
96
+ <RcSesButton
97
+ key={label}
98
+ variant='link'
99
+ disabled={disabled || actionDisabled}
100
+ startIcon={Icon ? <Icon size={20} /> : undefined}
101
+ onClick={stopAnd(onClick)}
102
+ sx={ACTION_BUTTON_SX}
103
+ >
104
+ {label}
105
+ </RcSesButton>
106
+ ),
107
+ )}
108
+ </Stack>
109
+ )
110
+ }
111
+
112
+ if (trailing.type === 'toggle') {
113
+ return (
114
+ <RcSesSwitch
115
+ checked={trailing.checked}
116
+ disabled={disabled}
117
+ ariaLabel={trailing['aria-label'] ?? t('toggle')}
118
+ onClick={stop}
119
+ onChange={withStop((e) => trailing.onChange(e.target.checked))}
120
+ />
121
+ )
122
+ }
123
+
124
+ if (trailing.type === 'status') {
125
+ return (
126
+ <RcSesBadge
127
+ label={trailing.label}
128
+ variant={STATUS_VARIANT_MAP[trailing.color ?? 'grey']}
129
+ size='small'
130
+ />
131
+ )
132
+ }
133
+
134
+ if (trailing.type === 'price') {
135
+ return (
136
+ <Stack
137
+ direction='row'
138
+ sx={[
139
+ {
140
+ alignItems: 'center',
141
+ gap: '0.5rem',
142
+ },
143
+ ...(Array.isArray(NO_SHRINK_SX) ? NO_SHRINK_SX : [NO_SHRINK_SX]),
144
+ ]}
145
+ >
146
+ <Box sx={PRICE_BLOCK_SX}>
147
+ <Typography
148
+ variant='body1'
149
+ sx={{ fontSize: '1rem', fontWeight: 600, lineHeight: '1.375rem' }}
150
+ color={palette.grey[900]}
151
+ >
152
+ {trailing.amount}
153
+ </Typography>
154
+ {!!trailing.caption && (
155
+ <Typography
156
+ variant='caption'
157
+ sx={{ fontSize: '0.8125rem', lineHeight: '1.125rem' }}
158
+ color={palette.grey[600]}
159
+ >
160
+ {trailing.caption}
161
+ </Typography>
162
+ )}
163
+ </Box>
164
+ {!!trailing.onDelete && (
165
+ <IconButton
166
+ aria-label={trailing.deleteAriaLabel ?? t('delete')}
167
+ size='small'
168
+ disabled={disabled}
169
+ onClick={stopAnd(() => trailing.onDelete?.())}
170
+ sx={{ padding: '0.5rem 0.25rem' }}
171
+ >
172
+ <TrashIcon size={20} fillColor={palette.grey[700]} />
173
+ </IconButton>
174
+ )}
175
+ </Stack>
176
+ )
177
+ }
178
+
179
+ if (trailing.type === 'checkbox') {
180
+ return (
181
+ <RcSesSimpleCheckbox
182
+ checked={trailing.checked}
183
+ disabled={disabled}
184
+ slotProps={{ input: { 'aria-label': trailing['aria-label'] } }}
185
+ onClick={stop}
186
+ onChange={withStop((e) => trailing.onChange(e.target.checked))}
187
+ sx={{ padding: 0, ...NO_SHRINK_SX }}
188
+ />
189
+ )
190
+ }
191
+ // change to RcSesRadio when it is implemented
192
+ if (trailing.type === 'radio') {
193
+ return (
194
+ <Radio
195
+ checked={trailing.checked}
196
+ disabled={disabled}
197
+ name={trailing.name}
198
+ slotProps={{ input: { 'aria-label': trailing['aria-label'] } }}
199
+ onClick={stop}
200
+ onChange={withStop((e) => trailing.onChange(e.target.checked))}
201
+ sx={{ padding: 0, ...NO_SHRINK_SX }}
202
+ />
203
+ )
204
+ }
205
+
206
+ if (trailing.type === 'chevron') {
207
+ return (
208
+ <Box sx={{ display: 'flex', flexShrink: 0 }} aria-hidden>
209
+ <CaretRightBoldIcon size={16} fillColor={palette.grey[500]} />
210
+ </Box>
211
+ )
212
+ }
213
+
214
+ if (trailing.type === 'stepper') {
215
+ const { value, onChange, min, max } = trailing
216
+ const atMin = min !== undefined && value <= min
217
+ const atMax = max !== undefined && value >= max
218
+
219
+ return (
220
+ <Stack
221
+ direction='row'
222
+ onClick={stop}
223
+ sx={[
224
+ {
225
+ alignItems: 'center',
226
+ gap: '0.25rem',
227
+ },
228
+ ...(Array.isArray(NO_SHRINK_SX) ? NO_SHRINK_SX : [NO_SHRINK_SX]),
229
+ ]}
230
+ >
231
+ <IconButton
232
+ aria-label={t('decrease')}
233
+ size='small'
234
+ disabled={disabled || atMin}
235
+ onClick={stopAnd(() => onChange(value - 1))}
236
+ sx={STEPPER_BUTTON_SX}
237
+ >
238
+ <MinusRegularIcon size={20} fillColor={palette.primary[700]} />
239
+ </IconButton>
240
+ <Typography
241
+ aria-label={trailing['aria-label']}
242
+ variant='body2'
243
+ sx={{ minWidth: '1.5rem', textAlign: 'center', fontWeight: 600 }}
244
+ >
245
+ {value}
246
+ </Typography>
247
+ <IconButton
248
+ aria-label={t('increase')}
249
+ size='small'
250
+ disabled={disabled || atMax}
251
+ onClick={stopAnd(() => onChange(value + 1))}
252
+ sx={STEPPER_BUTTON_SX}
253
+ >
254
+ <PlusRegularIcon size={20} fillColor={palette.primary[700]} />
255
+ </IconButton>
256
+ </Stack>
257
+ )
258
+ }
259
+
260
+ return null
261
+ }
262
+
263
+ export default AdvancedListItemTrailing
@@ -0,0 +1,80 @@
1
+ import { act, renderHook } from '@testing-library/react'
2
+ import { afterEach, describe, expect, test, vi } from 'vitest'
3
+
4
+ import usePrefersReducedMotion from './usePrefersReducedMotion'
5
+
6
+ const QUERY = '(prefers-reduced-motion: reduce)'
7
+
8
+ const createMatchMediaMock = (initialMatches: boolean) => {
9
+ let matches = initialMatches
10
+ const listeners = new Set<() => void>()
11
+
12
+ const mediaQueryList = {
13
+ get matches() {
14
+ return matches
15
+ },
16
+ addEventListener: vi.fn((_event: string, listener: () => void) => {
17
+ listeners.add(listener)
18
+ }),
19
+ removeEventListener: vi.fn((_event: string, listener: () => void) => {
20
+ listeners.delete(listener)
21
+ }),
22
+ }
23
+
24
+ return {
25
+ matchMedia: vi.fn(() => mediaQueryList),
26
+ mediaQueryList,
27
+ setMatches: (next: boolean) => {
28
+ matches = next
29
+ listeners.forEach((listener) => listener())
30
+ },
31
+ }
32
+ }
33
+
34
+ describe('usePrefersReducedMotion', () => {
35
+ afterEach(() => {
36
+ vi.unstubAllGlobals()
37
+ })
38
+
39
+ test('reflects the current matchMedia value on mount', () => {
40
+ const { matchMedia } = createMatchMediaMock(true)
41
+ vi.stubGlobal('matchMedia', matchMedia)
42
+
43
+ const { result } = renderHook(() => usePrefersReducedMotion())
44
+
45
+ expect(result.current).toBe(true)
46
+ expect(matchMedia).toHaveBeenCalledWith(QUERY)
47
+ })
48
+
49
+ test('updates when the OS-level preference changes', () => {
50
+ const { matchMedia, setMatches } = createMatchMediaMock(false)
51
+ vi.stubGlobal('matchMedia', matchMedia)
52
+
53
+ const { result } = renderHook(() => usePrefersReducedMotion())
54
+ expect(result.current).toBe(false)
55
+
56
+ act(() => setMatches(true))
57
+
58
+ expect(result.current).toBe(true)
59
+ })
60
+
61
+ test('unsubscribes the change listener on unmount', () => {
62
+ const { matchMedia, mediaQueryList } = createMatchMediaMock(false)
63
+ vi.stubGlobal('matchMedia', matchMedia)
64
+
65
+ const { unmount } = renderHook(() => usePrefersReducedMotion())
66
+ expect(mediaQueryList.addEventListener).toHaveBeenCalledTimes(1)
67
+
68
+ unmount()
69
+
70
+ expect(mediaQueryList.removeEventListener).toHaveBeenCalledTimes(1)
71
+ })
72
+
73
+ test('falls back to false when matchMedia is unsupported (SSR-safe)', () => {
74
+ vi.stubGlobal('matchMedia', undefined)
75
+
76
+ const { result } = renderHook(() => usePrefersReducedMotion())
77
+
78
+ expect(result.current).toBe(false)
79
+ })
80
+ })
@@ -0,0 +1,28 @@
1
+ import { useEffect, useState } from 'react'
2
+
3
+ const QUERY = '(prefers-reduced-motion: reduce)'
4
+
5
+ const supportsMatchMedia = () =>
6
+ typeof window !== 'undefined' && typeof window.matchMedia === 'function'
7
+
8
+ const usePrefersReducedMotion = () => {
9
+ const [prefersReducedMotion, setPrefersReducedMotion] = useState(
10
+ () => supportsMatchMedia() && window.matchMedia(QUERY).matches,
11
+ )
12
+
13
+ useEffect(() => {
14
+ if (!supportsMatchMedia()) {
15
+ return undefined
16
+ }
17
+
18
+ const mediaQueryList = window.matchMedia(QUERY)
19
+ const listener = () => setPrefersReducedMotion(mediaQueryList.matches)
20
+
21
+ mediaQueryList.addEventListener('change', listener)
22
+ return () => mediaQueryList.removeEventListener('change', listener)
23
+ }, [])
24
+
25
+ return prefersReducedMotion
26
+ }
27
+
28
+ export default usePrefersReducedMotion