w-design-system 0.0.57 → 0.0.59

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.
@@ -2,11 +2,13 @@ import React, {
2
2
  HTMLAttributes,
3
3
  PropsWithChildren,
4
4
  ReactNode,
5
+ useCallback,
5
6
  useEffect,
6
7
  useImperativeHandle,
7
8
  useMemo,
8
9
  useRef,
9
10
  useState,
11
+ PointerEvent,
10
12
  } from 'react'
11
13
  import { chevronDownSVG } from '../../assets/icon'
12
14
  import { IconWrapper } from '../IconWrapper'
@@ -14,7 +16,18 @@ import { Input } from '../Input'
14
16
  import { Paragraph } from '../Paragraph'
15
17
 
16
18
  import { theme } from '../Themes'
17
- import { Dropdown, DropdownWrapper, OptionButton, OptionText, OptionTextBadge, SelectWrapper } from './styles'
19
+ import {
20
+ Dropdown,
21
+ DropdownWrapper,
22
+ NoResults,
23
+ OptionBackground,
24
+ OptionBackgroundProps,
25
+ OptionButton,
26
+ OptionsWrapper,
27
+ OptionText,
28
+ OptionTextBadge,
29
+ SelectWrapper,
30
+ } from './styles'
18
31
 
19
32
  export interface SelectProps<
20
33
  T extends { text: string; id: number; icon?: ReactNode; badge?: string | number } = {
@@ -70,6 +83,37 @@ const SelectFowardRef = <T extends { text: string; id: number; icon?: ReactNode;
70
83
  setOpen,
71
84
  }))
72
85
 
86
+ const lastItemIdRef = useRef<number | undefined>()
87
+ const optionsWrapperRef = useRef<HTMLDivElement | null>(null)
88
+
89
+ const [backgroundStyle, setBackgroundStyle] = useState<OptionBackgroundProps>({
90
+ $opacity: 0,
91
+ $width: '0px',
92
+ $height: '0px',
93
+ $top: '0px',
94
+ $left: '0px',
95
+ })
96
+
97
+ const updateBackground = useCallback((index: number) => {
98
+ if (index === lastItemIdRef.current) return
99
+ const target = optionRefs.current[index]
100
+ if (!target) return
101
+ if (!optionsWrapperRef.current) return
102
+
103
+ const { left, top, height } = target.getBoundingClientRect()
104
+ const { left: contentLeft, top: contentTop } = optionsWrapperRef.current.getBoundingClientRect()
105
+ const padding = 16
106
+ setBackgroundStyle({
107
+ $opacity: 1,
108
+ $width: '100%',
109
+ $height: `${height}px`,
110
+ $top: `${padding + top - contentTop}px`,
111
+ $left: `${left - contentLeft}px`,
112
+ })
113
+
114
+ lastItemIdRef.current = index
115
+ }, [])
116
+
73
117
  useEffect(() => {
74
118
  if (!open) return
75
119
  const handler = (event: MouseEvent) => {
@@ -89,10 +133,10 @@ const SelectFowardRef = <T extends { text: string; id: number; icon?: ReactNode;
89
133
  }
90
134
  }, [open])
91
135
 
92
- const onOptionClick = (option: T) => {
136
+ const onOptionClick = useCallback((option: T) => {
93
137
  onOptionChange && onOptionChange(option)
94
138
  setOpen(false)
95
- }
139
+ }, [onOptionChange])
96
140
 
97
141
  useEffect(() => {
98
142
  if (usingInput) return
@@ -150,6 +194,7 @@ const SelectFowardRef = <T extends { text: string; id: number; icon?: ReactNode;
150
194
  useEffect(() => {
151
195
  if (!open) {
152
196
  setHighlightedIndex(null)
197
+ setBackgroundStyle({ ...backgroundStyle, $opacity: 0 })
153
198
  return
154
199
  }
155
200
 
@@ -161,6 +206,8 @@ const SelectFowardRef = <T extends { text: string; id: number; icon?: ReactNode;
161
206
  setHighlightedIndex(prevIndex => {
162
207
  const nextIndex = prevIndex === null || prevIndex === filteredOptions.length - 1 ? 0 : prevIndex + 1
163
208
  scrollIntoView(nextIndex)
209
+ updateBackground(nextIndex)
210
+
164
211
  return nextIndex
165
212
  })
166
213
  }
@@ -170,6 +217,7 @@ const SelectFowardRef = <T extends { text: string; id: number; icon?: ReactNode;
170
217
  setHighlightedIndex(prevIndex => {
171
218
  const nextIndex = prevIndex === null || prevIndex === 0 ? filteredOptions.length - 1 : prevIndex - 1
172
219
  scrollIntoView(nextIndex)
220
+ updateBackground(nextIndex)
173
221
  return nextIndex
174
222
  })
175
223
  }
@@ -180,19 +228,79 @@ const SelectFowardRef = <T extends { text: string; id: number; icon?: ReactNode;
180
228
  }
181
229
  }
182
230
 
183
- const scrollIntoView = (index: number) => {
184
- const option = optionRefs.current[index]
185
- if (option && dropdownRef.current) {
186
- option.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
187
- }
188
- }
189
-
190
231
  document.addEventListener('keydown', handleKeyDown)
191
232
 
192
233
  return () => {
193
234
  document.removeEventListener('keydown', handleKeyDown)
194
235
  }
195
- }, [open, filteredOptions, highlightedIndex])
236
+ }, [open, filteredOptions, highlightedIndex, updateBackground, backgroundStyle, onOptionClick])
237
+
238
+ /**
239
+ * When the user has the mouse over the dropdown and presses an up or down arrow key, the dropdown should
240
+ * ignore the hover change, as the user is navigating with the keyboard
241
+ */
242
+ const [disableHoverChange, setDisableHoverChange] = useState(false)
243
+ const timeoutRef = useRef<NodeJS.Timeout | null>(null)
244
+
245
+ const scrollIntoView = (index: number) => {
246
+ const option = optionRefs.current[index]
247
+ if (option && dropdownRef.current) {
248
+ option.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
249
+ if (timeoutRef.current) clearTimeout(timeoutRef.current)
250
+ const intersectionObserver = new IntersectionObserver(entries => {
251
+ if (entries[0].isIntersecting) {
252
+ setDisableHoverChange(true)
253
+ timeoutRef.current = setTimeout(() => {
254
+ timeoutRef.current = null
255
+ setDisableHoverChange(false)
256
+ }, 200)
257
+ }
258
+ setTimeout(() => {
259
+ intersectionObserver.disconnect()
260
+ }, 100)
261
+ })
262
+ if (dropdownRef.current) intersectionObserver.observe(dropdownRef.current)
263
+ }
264
+ }
265
+
266
+ useEffect(() => {
267
+ if (disableHoverChange && dropdownRef.current) {
268
+ const element = dropdownRef.current
269
+ const handler = () => {
270
+ setDisableHoverChange(false)
271
+ }
272
+ element.addEventListener('scrollend', handler)
273
+
274
+ return () => {
275
+ element.removeEventListener('scrollend', handler)
276
+ }
277
+ }
278
+ }, [disableHoverChange])
279
+ /**
280
+ * Used to handle the pointer enter event, it's used to highlight the option that the mouse is over
281
+ */
282
+ const onPointerEnter = useCallback(
283
+ (event: PointerEvent<Element>, index: number) => {
284
+ if (disableHoverChange) return
285
+ updateBackground(index)
286
+ setHighlightedIndex(index)
287
+ },
288
+ [disableHoverChange, updateBackground, setHighlightedIndex],
289
+ )
290
+
291
+ /**
292
+ * Used to handle the pointer leave event, it's used to remove the highlight from the option that the mouse is over
293
+ */
294
+ const onPointerLeave = useCallback(
295
+ (event: PointerEvent<Element>, index: number) => {
296
+ if (disableHoverChange) return
297
+ if (index === lastItemIdRef.current) {
298
+ setBackgroundStyle(currentStyle => ({ ...currentStyle, $opacity: 0 }))
299
+ lastItemIdRef.current = undefined
300
+ }
301
+ },
302
+ [disableHoverChange],
303
+ )
196
304
 
197
305
  return (
198
306
  <SelectWrapper {...props} ref={wrapperRef}>
@@ -220,49 +328,58 @@ const SelectFowardRef = <T extends { text: string; id: number; icon?: ReactNode;
220
328
  setComputedValue('')
221
329
  setUsingInput(true)
222
330
  setOpen(true)
331
+ setBackgroundStyle({ $opacity: 0, $width: '100%', $height: '0px', $top: '0px', $left: '0px' })
223
332
  }
224
333
  }}
225
334
  />
226
335
  {open && (
227
- <DropdownWrapper ref={dropdownRef}>
336
+ <DropdownWrapper>
228
337
  <Dropdown
338
+ ref={dropdownRef}
229
339
  dropDownTop={props.dropDownTop}
230
340
  dropDownWidth={dropDownWidth}
231
341
  dropDownMaxHeight={props.dropDownMaxHeight}
232
342
  >
233
343
  {(options?.length ?? 0) > 0 && (
234
- <div>
235
- {filteredOptions.map((option, index) => {
236
- const isSelected = selectedOption?.id === option.id
237
- const isHighlighted = highlightedIndex === index
238
-
239
- return (
240
- <OptionButton
241
- selected={isSelected}
242
- key={option.id}
243
- onClick={() => onOptionClick(option)}
244
- ref={el => (optionRefs.current[index] = el)}
245
- style={{
246
- backgroundColor: isHighlighted ? theme.colors.shade20 : 'transparent',
247
- }}
248
- >
249
- {option?.badge ? (
250
- <OptionTextBadge>
251
- <div>{option.text}</div>
252
- <Paragraph strongBod color={theme.colors.honey30}>
253
- {option.badge}
254
- </Paragraph>
255
- </OptionTextBadge>
256
- ) : (
257
- <>
258
- {option.icon}
259
- <OptionText>{option.text}</OptionText>
260
- </>
261
- )}
262
- </OptionButton>
263
- )
264
- })}
265
- </div>
344
+ <>
345
+ <OptionsWrapper ref={optionsWrapperRef}>
346
+ {filteredOptions.map((option, index) => {
347
+ const isSelected = selectedOption?.id === option.id
348
+ return (
349
+ <OptionButton
350
+ selected={isSelected}
351
+ key={option.id}
352
+ onClick={() => onOptionClick(option)}
353
+ ref={el => (optionRefs.current[index] = el)}
354
+ onPointerEnter={event => onPointerEnter(event, index)}
355
+ onPointerLeave={event => onPointerLeave(event, index)}
356
+ >
357
+ {option?.badge ? (
358
+ <OptionTextBadge>
359
+ <div>{option.text}</div>
360
+ <Paragraph strongBod color={theme.colors.honey30}>
361
+ {option.badge}
362
+ </Paragraph>
363
+ </OptionTextBadge>
364
+ ) : (
365
+ <>
366
+ {option.icon}
367
+ <OptionText>{option.text}</OptionText>
368
+ </>
369
+ )}
370
+ </OptionButton>
371
+ )
372
+ })}
373
+ </OptionsWrapper>
374
+ <OptionBackground
375
+ $opacity={backgroundStyle.$opacity}
376
+ $width={backgroundStyle.$width}
377
+ $height={backgroundStyle.$height}
378
+ $top={backgroundStyle.$top}
379
+ $left={backgroundStyle.$left}
380
+ />
381
+ {filteredOptions.length === 0 && options?.length && <NoResults>Sem resultados</NoResults>}
382
+ </>
266
383
  )}
267
384
  {children}
268
385
  </Dropdown>
@@ -69,9 +69,6 @@ export const OptionButton = styled.button<OptionButtonProps>`
69
69
  border-bottom: 1px solid ${theme.colors.shade10};
70
70
  }
71
71
 
72
- &:hover {
73
- color: ${theme.colors.cyan30};
74
- }
75
72
  `
76
73
 
77
74
  export const OptionText = styled.div`
@@ -83,3 +80,36 @@ export const OptionTextBadge = styled.div`
83
80
  width: 100%;
84
81
  justify-content: space-between;
85
82
  `
83
+
84
+ export const OptionsWrapper = styled.div`
85
+ position: relative;
86
+ overflow-y: auto;
87
+ `
88
+
89
+ export interface OptionBackgroundProps {
90
+ $opacity: number
91
+ $width: string
92
+ $height: string
93
+ $top: string
94
+ $left: string
95
+ }
96
+
97
+ export const OptionBackground = styled.div<OptionBackgroundProps>`
98
+ position: absolute;
99
+ z-index: -1;
100
+ background-color: ${theme.colors.shade10};
101
+ opacity: ${({ $opacity }) => $opacity};
102
+ width: ${({ $width }) => $width};
103
+ height: ${({ $height }) => $height};
104
+ top: ${({ $top }) => $top};
105
+ left: ${({ $left }) => $left};
106
+ transition: all 0.15s;
107
+ `
108
+
109
+ export const NoResults = styled.div`
110
+ color: ${theme.colors.shade40};
111
+ font-size: ${theme.paragraph.medium};
112
+ font-family: 'Nunito Sans', sans-serif;
113
+ padding: 16px;
114
+ text-align: center;
115
+ `
@@ -94,7 +94,8 @@ export const Sidebar = ({
94
94
  )
95
95
  if (item.type === 'group') {
96
96
  const group = groups[item.id]
97
- if (group.itemIds == null || group.itemIds.length === 0) {
97
+ const itemShowInMenu = group.itemIds?.some(id => items[id]?.showInMenu !== false)
98
+ if (group.itemIds == null || group.itemIds.length === 0 || !itemShowInMenu) {
98
99
  const itemRouter = items[item.id]
99
100
  return <SidebarItem
100
101
  key={item.id}
@@ -90,6 +90,7 @@ export const Playground: Story = {
90
90
  icon: dashboardSVG,
91
91
  link: '/dashboard',
92
92
  disabled: false,
93
+ showInMenu: false
93
94
  }
94
95
  }}
95
96
  currentItemId={args.currentItemId}
@@ -99,7 +100,7 @@ export const Playground: Story = {
99
100
  DASHBOARD: {
100
101
  name: 'Group 1',
101
102
  icon: dashboardSVG,
102
- itemIds: [],
103
+ itemIds: ['DASHBOARD'],
103
104
  },
104
105
  }}
105
106
  logoUrl={''}
@@ -1,79 +0,0 @@
1
- import styled from 'styled-components'
2
- import { theme } from '../Themes'
3
-
4
- export const MultiSelectWrapper = styled.div`
5
- display: flex;
6
- flex-direction: column;
7
- position: relative;
8
- .icon {
9
- transition: transform 0.3s;
10
- }
11
- .icon-rotate {
12
- transform: rotate(180deg) translateY(5px);
13
- }
14
- `
15
- export interface DropdownProps {
16
- dropDownTop?: boolean
17
- dropDownWidth?: string
18
- dropDownMaxHeight?: string
19
- }
20
-
21
- export const Dropdown = styled.div<DropdownProps>`
22
- position: absolute;
23
-
24
- z-index: ${theme.zIndex.z9};
25
-
26
- background-color: ${theme.colors.white};
27
- width: ${({ dropDownWidth }) => (dropDownWidth ? dropDownWidth : '100%')};
28
- ${({ dropDownTop }) => (dropDownTop ? 'bottom: 58px;' : 'margin-top: 4px;')};
29
- padding: 16px;
30
-
31
- border-radius: 4px;
32
- border: 1px solid ${theme.colors.shade20};
33
- box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.05);
34
- height: auto;
35
- max-height: ${({ dropDownMaxHeight }) => (dropDownMaxHeight ? dropDownMaxHeight : '300px')};
36
- overflow-y: auto;
37
- `
38
-
39
- export const DropdownWrapper = styled.div`
40
- position: relative;
41
- z-index: ${theme.zIndex.z9};
42
- `
43
-
44
- export interface OptionButtonProps {
45
- selected: boolean
46
- }
47
-
48
- export const OptionButton = styled.button<OptionButtonProps>`
49
- display: flex;
50
-
51
- align-items: center;
52
- justify-content: space-between;
53
- gap: 0.5rem;
54
- color: ${({ selected }) => (selected ? theme.colors.cyan30 : theme.colors.shade40)};
55
- background-color: transparent;
56
- border: none;
57
-
58
- cursor: pointer;
59
-
60
- font-family: 'Nunito Sans', sans-serif;
61
- font-size: ${theme.paragraph.medium};
62
- text-align: left;
63
-
64
- height: 46px;
65
- width: 100%;
66
- padding: 0;
67
-
68
- &:not(:last-child) {
69
- border-bottom: 1px solid ${theme.colors.shade10};
70
- }
71
-
72
- &:hover {
73
- color: ${theme.colors.cyan30};
74
- }
75
- `
76
-
77
- export const OptionText = styled.div`
78
- flex-grow: 1;
79
- `