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

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 (94) 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/components/form/inputs/Select/index.d.ts +1 -0
  21. package/dist/library/icons.d.ts +9 -1
  22. package/dist/library/index.d.ts +5 -0
  23. package/dist/library/types.d.ts +2 -1
  24. package/dist/registrucentras-rc-ses-react-components.cjs +1 -1
  25. package/dist/registrucentras-rc-ses-react-components.js +4137 -3249
  26. package/dist/stories/components/actions/Button.stories.d.ts +4 -3
  27. package/dist/stories/components/display/AdvancedList.stories.d.ts +33 -0
  28. package/dist/stories/components/display/AdvancedListItem.stories.d.ts +33 -0
  29. package/dist/stories/components/display/SelectableCardList.stories.d.ts +8 -1
  30. package/dist/stories/components/display/Tooltip.stories.d.ts +48 -0
  31. package/dist/stories/components/inputs/DatePicker.stories.d.ts +23 -0
  32. package/dist/stories/components/loaders/LoadingSpinner.stories.d.ts +3 -3
  33. package/dist/stories/components/overlays/Dialog.stories.d.ts +6 -0
  34. package/dist/stories/components/overlays/Popover.stories.d.ts +12 -0
  35. package/dist/stories/theme/ThemedMuiComponents.stories.d.ts +25 -0
  36. package/dist/theme/light/MuiTooltip.d.ts +3 -0
  37. package/dist/theme/motion.d.ts +16 -0
  38. package/package.json +2 -1
  39. package/src/assets/icons/CaretUpBoldIcon.tsx +17 -0
  40. package/src/assets/icons/CaretUpIcon.tsx +9 -0
  41. package/src/assets/icons/ListIcon.tsx +17 -0
  42. package/src/assets/icons/MinusIcon.tsx +9 -0
  43. package/src/assets/icons/NotePencilIcon.tsx +9 -0
  44. package/src/assets/icons/PlusIcon.tsx +9 -0
  45. package/src/assets/icons/TrashIcon.tsx +23 -0
  46. package/src/assets/icons/UserIcon.tsx +17 -0
  47. package/src/components/common/AdvancedList/components/AdvancedListItem/AdvancedListItem.types.ts +100 -0
  48. package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemLeading.tsx +228 -0
  49. package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.tsx +270 -0
  50. package/src/components/common/AdvancedList/components/AdvancedListItem/hooks/usePrefersReducedMotion.test.ts +80 -0
  51. package/src/components/common/AdvancedList/components/AdvancedListItem/hooks/usePrefersReducedMotion.ts +28 -0
  52. package/src/components/common/AdvancedList/components/AdvancedListItem/index.test.tsx +631 -0
  53. package/src/components/common/AdvancedList/components/AdvancedListItem/index.tsx +340 -0
  54. package/src/components/common/AdvancedList/index.test.tsx +97 -0
  55. package/src/components/common/AdvancedList/index.tsx +52 -0
  56. package/src/components/common/Button/index.tsx +15 -1
  57. package/src/components/common/Card/index.tsx +2 -2
  58. package/src/components/common/ListWithIcons/components/ListWithIconsItem.tsx +7 -5
  59. package/src/components/common/SelectableCardList/components/SelectableCardListItem.test.tsx +50 -0
  60. package/src/components/common/SelectableCardList/components/SelectableCardListItem.tsx +34 -0
  61. package/src/components/common/SelectableCardList/index.test.tsx +56 -2
  62. package/src/components/common/SelectableCardList/index.tsx +29 -10
  63. package/src/components/common/Tooltip/index.test.tsx +85 -0
  64. package/src/components/common/Tooltip/index.tsx +168 -0
  65. package/src/components/form/inputs/FileDropzone.tsx +1 -0
  66. package/src/components/form/inputs/FileUpload.tsx +1 -0
  67. package/src/components/form/inputs/NumberStepper.tsx +1 -0
  68. package/src/components/form/inputs/PhoneInputFormControl/index.tsx +1 -1
  69. package/src/components/form/inputs/RadioButtonGroup.test.tsx +71 -0
  70. package/src/components/form/inputs/RadioButtonGroup.tsx +1 -0
  71. package/src/components/form/inputs/Select/index.test.tsx +159 -0
  72. package/src/components/form/inputs/Select/index.tsx +107 -52
  73. package/src/components/layout/Footer/index.test.tsx +13 -0
  74. package/src/components/layout/Footer/index.tsx +5 -2
  75. package/src/i18n/namespaces/common/en.json +14 -0
  76. package/src/i18n/namespaces/common/lt.json +14 -0
  77. package/src/library/icons.ts +16 -0
  78. package/src/library/index.ts +5 -0
  79. package/src/library/types.ts +2 -0
  80. package/src/setupTests.ts +7 -1
  81. package/src/stories/components/display/AdvancedList.stories.tsx +660 -0
  82. package/src/stories/components/display/AdvancedListItem.stories.tsx +653 -0
  83. package/src/stories/components/display/SelectableCardList.stories.tsx +19 -0
  84. package/src/stories/components/display/Tooltip.stories.tsx +67 -0
  85. package/src/stories/components/inputs/DatePicker.stories.tsx +36 -1
  86. package/src/stories/components/inputs/Select.stories.tsx +38 -2
  87. package/src/stories/components/overlays/Dialog.stories.tsx +34 -0
  88. package/src/stories/components/overlays/Popover.stories.tsx +65 -0
  89. package/src/stories/theme/ThemedMuiComponents.stories.tsx +177 -0
  90. package/src/theme/light/MuiAutocomplete.ts +3 -1
  91. package/src/theme/light/MuiInputBase.ts +4 -0
  92. package/src/theme/light/MuiTooltip.ts +40 -0
  93. package/src/theme/light/index.tsx +2 -0
  94. package/src/theme/motion.tsx +10 -0
@@ -0,0 +1,228 @@
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={{
161
+ color: palette.grey[600],
162
+ fontSize: '0.9375rem',
163
+ fontWeight: 400,
164
+ lineHeight: '1.375rem',
165
+ }}
166
+ >
167
+ {leading.index}
168
+ </Typography>
169
+ </Box>
170
+ )
171
+ }
172
+
173
+ if (leading.type === 'checkbox') {
174
+ return (
175
+ <RcSesSimpleCheckbox
176
+ checked={leading.checked}
177
+ disabled={disabled}
178
+ // MUI 9 replaced inputRef/inputProps with the `input` slot.
179
+ slotProps={{
180
+ input: {
181
+ ref: inputRef,
182
+ ...getAccessibleNameProps(leading['aria-label'], titleId),
183
+ ...(expandableId && {
184
+ 'aria-expanded': expanded,
185
+ 'aria-controls': expandableId,
186
+ }),
187
+ },
188
+ }}
189
+ onClick={stop}
190
+ onChange={(e) => {
191
+ stop(e)
192
+ leading.onChange(e.target.checked)
193
+ }}
194
+ sx={{ padding: 0 }}
195
+ />
196
+ )
197
+ }
198
+ // change to RcSesRadio when it is implemented
199
+ if (leading.type === 'radio') {
200
+ return (
201
+ <Radio
202
+ checked={leading.checked}
203
+ disabled={disabled}
204
+ name={leading.name}
205
+ slotProps={{
206
+ input: {
207
+ ref: inputRef,
208
+ ...getAccessibleNameProps(leading['aria-label'], titleId),
209
+ ...(expandableId && {
210
+ 'aria-expanded': expanded,
211
+ 'aria-controls': expandableId,
212
+ }),
213
+ },
214
+ }}
215
+ onClick={stop}
216
+ onChange={(e) => {
217
+ stop(e)
218
+ leading.onChange(e.target.checked)
219
+ }}
220
+ sx={{ padding: 0 }}
221
+ />
222
+ )
223
+ }
224
+
225
+ return null
226
+ }
227
+
228
+ export default AdvancedListItemLeading
@@ -0,0 +1,270 @@
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={{
150
+ color: palette.grey[900],
151
+ fontSize: '1rem',
152
+ fontWeight: 600,
153
+ lineHeight: '1.375rem',
154
+ }}
155
+ >
156
+ {trailing.amount}
157
+ </Typography>
158
+ {!!trailing.caption && (
159
+ <Typography
160
+ variant='caption'
161
+ sx={{
162
+ color: palette.grey[600],
163
+ fontSize: '0.8125rem',
164
+ lineHeight: '1.125rem',
165
+ }}
166
+ >
167
+ {trailing.caption}
168
+ </Typography>
169
+ )}
170
+ </Box>
171
+ {!!trailing.onDelete && (
172
+ <IconButton
173
+ aria-label={trailing.deleteAriaLabel ?? t('delete')}
174
+ size='small'
175
+ disabled={disabled}
176
+ onClick={stopAnd(() => trailing.onDelete?.())}
177
+ sx={{ padding: '0.5rem 0.25rem' }}
178
+ >
179
+ <TrashIcon size={20} fillColor={palette.grey[700]} />
180
+ </IconButton>
181
+ )}
182
+ </Stack>
183
+ )
184
+ }
185
+
186
+ if (trailing.type === 'checkbox') {
187
+ return (
188
+ <RcSesSimpleCheckbox
189
+ checked={trailing.checked}
190
+ disabled={disabled}
191
+ slotProps={{ input: { 'aria-label': trailing['aria-label'] } }}
192
+ onClick={stop}
193
+ onChange={withStop((e) => trailing.onChange(e.target.checked))}
194
+ sx={{ padding: 0, ...NO_SHRINK_SX }}
195
+ />
196
+ )
197
+ }
198
+ // change to RcSesRadio when it is implemented
199
+ if (trailing.type === 'radio') {
200
+ return (
201
+ <Radio
202
+ checked={trailing.checked}
203
+ disabled={disabled}
204
+ name={trailing.name}
205
+ slotProps={{ input: { 'aria-label': trailing['aria-label'] } }}
206
+ onClick={stop}
207
+ onChange={withStop((e) => trailing.onChange(e.target.checked))}
208
+ sx={{ padding: 0, ...NO_SHRINK_SX }}
209
+ />
210
+ )
211
+ }
212
+
213
+ if (trailing.type === 'chevron') {
214
+ return (
215
+ <Box sx={{ display: 'flex', flexShrink: 0 }} aria-hidden>
216
+ <CaretRightBoldIcon size={16} fillColor={palette.grey[500]} />
217
+ </Box>
218
+ )
219
+ }
220
+
221
+ if (trailing.type === 'stepper') {
222
+ const { value, onChange, min, max } = trailing
223
+ const atMin = min !== undefined && value <= min
224
+ const atMax = max !== undefined && value >= max
225
+
226
+ return (
227
+ <Stack
228
+ direction='row'
229
+ onClick={stop}
230
+ sx={[
231
+ {
232
+ alignItems: 'center',
233
+ gap: '0.25rem',
234
+ },
235
+ ...(Array.isArray(NO_SHRINK_SX) ? NO_SHRINK_SX : [NO_SHRINK_SX]),
236
+ ]}
237
+ >
238
+ <IconButton
239
+ aria-label={t('decrease')}
240
+ size='small'
241
+ disabled={disabled || atMin}
242
+ onClick={stopAnd(() => onChange(value - 1))}
243
+ sx={STEPPER_BUTTON_SX}
244
+ >
245
+ <MinusRegularIcon size={20} fillColor={palette.primary[700]} />
246
+ </IconButton>
247
+ <Typography
248
+ aria-label={trailing['aria-label']}
249
+ variant='body2'
250
+ sx={{ minWidth: '1.5rem', textAlign: 'center', fontWeight: 600 }}
251
+ >
252
+ {value}
253
+ </Typography>
254
+ <IconButton
255
+ aria-label={t('increase')}
256
+ size='small'
257
+ disabled={disabled || atMax}
258
+ onClick={stopAnd(() => onChange(value + 1))}
259
+ sx={STEPPER_BUTTON_SX}
260
+ >
261
+ <PlusRegularIcon size={20} fillColor={palette.primary[700]} />
262
+ </IconButton>
263
+ </Stack>
264
+ )
265
+ }
266
+
267
+ return null
268
+ }
269
+
270
+ 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