@faststore/ui 1.6.14 → 1.6.17
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.
- package/CHANGELOG.md +30 -0
- package/dist/molecules/Dropdown/Dropdown.d.ts +9 -0
- package/dist/molecules/Dropdown/DropdownButton.d.ts +10 -0
- package/dist/molecules/Dropdown/DropdownItem.d.ts +10 -0
- package/dist/molecules/Dropdown/DropdownMenu.d.ts +22 -0
- package/dist/molecules/Dropdown/contexts/DropdownContext.d.ts +41 -0
- package/dist/molecules/Dropdown/hooks/useDropdown.d.ts +6 -0
- package/dist/molecules/Dropdown/hooks/useDropdownPosition.d.ts +11 -0
- package/dist/molecules/Dropdown/index.d.ts +8 -0
- package/dist/molecules/Dropdown/stories/Dropdown.stories.d.ts +7 -0
- package/dist/molecules/Dropdown/stories/assets/Caret.d.ts +2 -0
- package/dist/ui.cjs.development.js +2 -3
- package/dist/ui.cjs.development.js.map +1 -1
- package/dist/ui.cjs.production.min.js +1 -1
- package/dist/ui.cjs.production.min.js.map +1 -1
- package/dist/ui.esm.js +2 -3
- package/dist/ui.esm.js.map +1 -1
- package/package.json +3 -3
- package/src/molecules/Dropdown/Dropdown.test.tsx +159 -0
- package/src/molecules/Dropdown/Dropdown.tsx +65 -0
- package/src/molecules/Dropdown/DropdownButton.tsx +42 -0
- package/src/molecules/Dropdown/DropdownItem.tsx +72 -0
- package/src/molecules/Dropdown/DropdownMenu.tsx +156 -0
- package/src/molecules/Dropdown/contexts/DropdownContext.ts +53 -0
- package/src/molecules/Dropdown/hooks/useDropdown.ts +18 -0
- package/src/molecules/Dropdown/hooks/useDropdownPosition.ts +26 -0
- package/src/molecules/Dropdown/index.ts +11 -0
- package/src/molecules/Dropdown/stories/Dropdown.mdx +51 -0
- package/src/molecules/Dropdown/stories/Dropdown.stories.tsx +94 -0
- package/src/molecules/Dropdown/stories/assets/Caret.tsx +19 -0
- package/src/molecules/Modal/ModalContent.tsx +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/ui",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.17",
|
|
4
4
|
"description": "A lightweight, framework agnostic component library for React",
|
|
5
5
|
"author": "emersonlaurentino",
|
|
6
6
|
"license": "MIT",
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
"@types/jest-axe": "^3.5.3",
|
|
77
77
|
"@types/tabbable": "^3.1.1",
|
|
78
78
|
"@types/testing-library__jest-dom": "^5.9.5",
|
|
79
|
-
"@vtex/theme-b2c-tailwind": "^1.6.
|
|
79
|
+
"@vtex/theme-b2c-tailwind": "^1.6.17",
|
|
80
80
|
"@vtex/tsconfig": "^0.5.0",
|
|
81
81
|
"chalk": "^5.0.0",
|
|
82
82
|
"jest-axe": "^5.0.1",
|
|
@@ -89,5 +89,5 @@
|
|
|
89
89
|
"tsdx": "^0.14.1",
|
|
90
90
|
"typescript": "^4.2.4"
|
|
91
91
|
},
|
|
92
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "42cb900adfa51975493e95481f1a1e7be8a1be74"
|
|
93
93
|
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { fireEvent, render } from '@testing-library/react'
|
|
2
|
+
import { axe } from 'jest-axe'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
|
|
5
|
+
import Dropdown, { DropdownButton, DropdownItem, DropdownMenu } from '.'
|
|
6
|
+
|
|
7
|
+
type SimpleDropdownProps = {
|
|
8
|
+
onDismiss?(): void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const SimpleDropdown = ({ onDismiss }: SimpleDropdownProps) => (
|
|
12
|
+
<Dropdown onDismiss={onDismiss}>
|
|
13
|
+
<DropdownButton>Dropdown Button</DropdownButton>
|
|
14
|
+
<DropdownMenu>
|
|
15
|
+
<DropdownItem>Dropdown Item 1</DropdownItem>
|
|
16
|
+
<DropdownItem>Dropdown Item 2</DropdownItem>
|
|
17
|
+
<DropdownItem>Dropdown Item 3</DropdownItem>
|
|
18
|
+
</DropdownMenu>
|
|
19
|
+
</Dropdown>
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
describe('Dropdown', () => {
|
|
23
|
+
it('Should render the DropdownButton component', () => {
|
|
24
|
+
const { getByText, queryByTestId } = render(<SimpleDropdown />)
|
|
25
|
+
|
|
26
|
+
expect(getByText(/Dropdown Button/g)).toBeInTheDocument()
|
|
27
|
+
expect(queryByTestId('store-dropdown-menu')).not.toBeInTheDocument()
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('Should render DropdownMenu when DropdownButton is clicked', () => {
|
|
31
|
+
const { getByText, queryByTestId } = render(<SimpleDropdown />)
|
|
32
|
+
|
|
33
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
34
|
+
|
|
35
|
+
fireEvent.click(dropdownButton)
|
|
36
|
+
expect(queryByTestId('store-dropdown-menu')).toBeInTheDocument()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('Should render 3 dropdownItems when DropdownMenu is opened', () => {
|
|
40
|
+
const { getByText, queryAllByTestId } = render(<SimpleDropdown />)
|
|
41
|
+
|
|
42
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
43
|
+
|
|
44
|
+
fireEvent.click(dropdownButton)
|
|
45
|
+
|
|
46
|
+
expect(queryAllByTestId('store-dropdown-item')).toHaveLength(3)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('Should close menu and emit onDismiss event when Overlay is clicked', () => {
|
|
50
|
+
const onDismissMock = jest.fn()
|
|
51
|
+
const { getByText, queryByTestId, getByTestId } = render(
|
|
52
|
+
<SimpleDropdown onDismiss={onDismissMock} />
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
56
|
+
|
|
57
|
+
fireEvent.click(dropdownButton)
|
|
58
|
+
|
|
59
|
+
const dropdownMenu = queryByTestId('store-dropdown-menu')
|
|
60
|
+
|
|
61
|
+
expect(dropdownMenu).toBeInTheDocument()
|
|
62
|
+
|
|
63
|
+
const overlay = getByTestId('store-overlay')
|
|
64
|
+
|
|
65
|
+
fireEvent.click(overlay)
|
|
66
|
+
|
|
67
|
+
expect(dropdownMenu).not.toBeInTheDocument()
|
|
68
|
+
expect(onDismissMock).toHaveBeenCalled()
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe('Accessibility', () => {
|
|
73
|
+
const getDropdownStructures = () => {
|
|
74
|
+
const { getByText, queryAllByTestId, getByTestId, queryByTestId } = render(
|
|
75
|
+
<SimpleDropdown />
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
79
|
+
|
|
80
|
+
fireEvent.click(dropdownButton)
|
|
81
|
+
|
|
82
|
+
const dropdownItems = queryAllByTestId('store-dropdown-item')
|
|
83
|
+
const overlay = getByTestId('store-overlay')
|
|
84
|
+
const menu = queryByTestId('store-dropdown-menu')
|
|
85
|
+
|
|
86
|
+
return { dropdownItems, overlay, menu }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
it('Should not have violations', async () => {
|
|
90
|
+
const { getByText, getByTestId, container } = render(<SimpleDropdown />)
|
|
91
|
+
|
|
92
|
+
const dropdownButton = getByText(/Dropdown Button/g)
|
|
93
|
+
|
|
94
|
+
fireEvent.click(dropdownButton)
|
|
95
|
+
|
|
96
|
+
const menu = getByTestId('store-dropdown-menu')
|
|
97
|
+
|
|
98
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
99
|
+
expect(await axe(container)).toHaveNoIncompletes()
|
|
100
|
+
expect(await axe(menu)).toHaveNoViolations()
|
|
101
|
+
expect(await axe(menu)).toHaveNoIncompletes()
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('Should close Dropdown menu when Escape key is pressed', async () => {
|
|
105
|
+
const { overlay, menu } = getDropdownStructures()
|
|
106
|
+
|
|
107
|
+
fireEvent.keyDown(overlay, {
|
|
108
|
+
key: 'Escape',
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
expect(menu).not.toBeInTheDocument()
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('Should focus on second DropdownItem when ArrowDown key is pressed', async () => {
|
|
115
|
+
const { overlay, dropdownItems } = getDropdownStructures()
|
|
116
|
+
|
|
117
|
+
fireEvent.keyDown(overlay, {
|
|
118
|
+
key: 'ArrowDown',
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
expect(dropdownItems[1]).toHaveFocus()
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('Should focus on second DropdownItem when ArrowUp key is pressed twice', () => {
|
|
125
|
+
const { overlay, dropdownItems } = getDropdownStructures()
|
|
126
|
+
|
|
127
|
+
fireEvent.keyDown(overlay, {
|
|
128
|
+
key: 'ArrowUp',
|
|
129
|
+
})
|
|
130
|
+
fireEvent.keyDown(overlay, {
|
|
131
|
+
key: 'ArrowUp',
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
expect(dropdownItems[1]).toHaveFocus()
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('Should focus on last DropdownItem when End key is pressed', () => {
|
|
138
|
+
const { overlay, dropdownItems } = getDropdownStructures()
|
|
139
|
+
|
|
140
|
+
fireEvent.keyDown(overlay, {
|
|
141
|
+
key: 'End',
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
expect(dropdownItems[2]).toHaveFocus()
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('Should focus on first DropdownItem when Home key is pressed', () => {
|
|
148
|
+
const { overlay, dropdownItems } = getDropdownStructures()
|
|
149
|
+
|
|
150
|
+
fireEvent.keyDown(overlay, {
|
|
151
|
+
key: 'ArrowUp',
|
|
152
|
+
})
|
|
153
|
+
fireEvent.keyDown(overlay, {
|
|
154
|
+
key: 'Home',
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
expect(dropdownItems[0]).toHaveFocus()
|
|
158
|
+
})
|
|
159
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
import React, { useRef, useMemo, useState, useEffect } from 'react'
|
|
3
|
+
|
|
4
|
+
import DropdownContext from './contexts/DropdownContext'
|
|
5
|
+
|
|
6
|
+
export type DropdownProps = {
|
|
7
|
+
children: ReactNode
|
|
8
|
+
onDismiss?(): void
|
|
9
|
+
isOpen?: boolean
|
|
10
|
+
id?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const Dropdown = ({
|
|
14
|
+
children,
|
|
15
|
+
isOpen: isOpenDefault = false,
|
|
16
|
+
onDismiss,
|
|
17
|
+
id = 'store-dropdown',
|
|
18
|
+
}: DropdownProps) => {
|
|
19
|
+
const [isOpen, setIsOpen] = useState(isOpenDefault)
|
|
20
|
+
const dropdownItemsRef = useRef<HTMLButtonElement[]>([])
|
|
21
|
+
const selectedDropdownItemIndexRef = useRef(0)
|
|
22
|
+
const dropdownButtonRef = useRef<HTMLButtonElement>(null)
|
|
23
|
+
|
|
24
|
+
const close = () => {
|
|
25
|
+
setIsOpen(false)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const open = () => {
|
|
29
|
+
setIsOpen(true)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const toggle = () => {
|
|
33
|
+
setIsOpen((old) => !old)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
setIsOpen(isOpenDefault)
|
|
38
|
+
}, [isOpenDefault])
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
isOpen && dropdownItemsRef?.current[0]?.focus()
|
|
42
|
+
}, [isOpen])
|
|
43
|
+
|
|
44
|
+
const value = useMemo(() => {
|
|
45
|
+
return {
|
|
46
|
+
isOpen,
|
|
47
|
+
close,
|
|
48
|
+
open,
|
|
49
|
+
toggle,
|
|
50
|
+
dropdownButtonRef,
|
|
51
|
+
onDismiss,
|
|
52
|
+
selectedDropdownItemIndexRef,
|
|
53
|
+
dropdownItemsRef,
|
|
54
|
+
id,
|
|
55
|
+
}
|
|
56
|
+
}, [id, isOpen, onDismiss])
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<DropdownContext.Provider value={value}>
|
|
60
|
+
{children}
|
|
61
|
+
</DropdownContext.Provider>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default Dropdown
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ButtonHTMLAttributes } from 'react'
|
|
2
|
+
import React, { forwardRef, useImperativeHandle } from 'react'
|
|
3
|
+
|
|
4
|
+
import { useDropdown } from './hooks/useDropdown'
|
|
5
|
+
|
|
6
|
+
export interface DropdownButtonProps
|
|
7
|
+
extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
8
|
+
/**
|
|
9
|
+
* ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
|
|
10
|
+
*/
|
|
11
|
+
testId?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const DropdownButton = forwardRef<HTMLButtonElement, DropdownButtonProps>(
|
|
15
|
+
function Button(
|
|
16
|
+
{ children, testId = 'store-dropdown-button', ...otherProps },
|
|
17
|
+
ref
|
|
18
|
+
) {
|
|
19
|
+
const { toggle, dropdownButtonRef, isOpen, id } = useDropdown()
|
|
20
|
+
|
|
21
|
+
useImperativeHandle(ref, () => dropdownButtonRef!.current!, [
|
|
22
|
+
dropdownButtonRef,
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<button
|
|
27
|
+
data-store-dropdown-button
|
|
28
|
+
onClick={toggle}
|
|
29
|
+
data-testid={testId}
|
|
30
|
+
ref={dropdownButtonRef}
|
|
31
|
+
aria-expanded={isOpen}
|
|
32
|
+
aria-haspopup="menu"
|
|
33
|
+
aria-controls={id}
|
|
34
|
+
{...otherProps}
|
|
35
|
+
>
|
|
36
|
+
{children}
|
|
37
|
+
</button>
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
export default DropdownButton
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { ButtonHTMLAttributes } from 'react'
|
|
2
|
+
import React, { useImperativeHandle, forwardRef, useRef, useState } from 'react'
|
|
3
|
+
|
|
4
|
+
import { useDropdown } from './hooks/useDropdown'
|
|
5
|
+
|
|
6
|
+
export interface DropdownItemProps
|
|
7
|
+
extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
8
|
+
/**
|
|
9
|
+
* ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
|
|
10
|
+
*/
|
|
11
|
+
testId?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const DropdownItem = forwardRef<HTMLButtonElement, DropdownItemProps>(
|
|
15
|
+
function Button(
|
|
16
|
+
{ children, onClick, testId = 'store-dropdown-item', ...otherProps },
|
|
17
|
+
ref
|
|
18
|
+
) {
|
|
19
|
+
const {
|
|
20
|
+
dropdownItemsRef,
|
|
21
|
+
selectedDropdownItemIndexRef,
|
|
22
|
+
close,
|
|
23
|
+
} = useDropdown()
|
|
24
|
+
|
|
25
|
+
const [dropdownItemIndex, setDropdownItemIndex] = useState(0)
|
|
26
|
+
const dropdownItemRef = useRef<HTMLButtonElement>()
|
|
27
|
+
|
|
28
|
+
const addToRefs = (el: HTMLButtonElement) => {
|
|
29
|
+
if (el && !dropdownItemsRef?.current.includes(el)) {
|
|
30
|
+
dropdownItemsRef?.current.push(el)
|
|
31
|
+
setDropdownItemIndex(
|
|
32
|
+
dropdownItemsRef?.current.findIndex((element) => element === el) ?? 0
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
dropdownItemRef.current = el
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const onFocusItem = () => {
|
|
40
|
+
selectedDropdownItemIndexRef!.current = dropdownItemIndex
|
|
41
|
+
dropdownItemsRef?.current[selectedDropdownItemIndexRef!.current]?.focus()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const handleOnClickItem = (
|
|
45
|
+
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
|
|
46
|
+
) => {
|
|
47
|
+
onClick?.(event)
|
|
48
|
+
close?.()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
useImperativeHandle(ref, () => dropdownItemRef.current!, [])
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<button
|
|
55
|
+
data-store-dropdown-item
|
|
56
|
+
data-testid={testId}
|
|
57
|
+
ref={addToRefs}
|
|
58
|
+
onFocus={onFocusItem}
|
|
59
|
+
onMouseEnter={onFocusItem}
|
|
60
|
+
onClick={handleOnClickItem}
|
|
61
|
+
role="menuitem"
|
|
62
|
+
tabIndex={-1}
|
|
63
|
+
data-index={dropdownItemIndex}
|
|
64
|
+
{...otherProps}
|
|
65
|
+
>
|
|
66
|
+
{children}
|
|
67
|
+
</button>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
export default DropdownItem
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AriaAttributes,
|
|
3
|
+
KeyboardEvent,
|
|
4
|
+
MouseEvent,
|
|
5
|
+
PropsWithChildren,
|
|
6
|
+
ReactElement,
|
|
7
|
+
} from 'react'
|
|
8
|
+
import React from 'react'
|
|
9
|
+
import { createPortal } from 'react-dom'
|
|
10
|
+
|
|
11
|
+
import Overlay from '../../atoms/Overlay'
|
|
12
|
+
import type { ModalContentProps } from '../Modal/ModalContent'
|
|
13
|
+
import { useDropdown } from './hooks/useDropdown'
|
|
14
|
+
import { useDropdownPosition } from './hooks/useDropdownPosition'
|
|
15
|
+
|
|
16
|
+
export interface DropdownMenuProps extends ModalContentProps {
|
|
17
|
+
/**
|
|
18
|
+
* ID to find this component in testing tools (e.g.: cypress, testing library, and jest).
|
|
19
|
+
*/
|
|
20
|
+
testId?: string
|
|
21
|
+
/**
|
|
22
|
+
* Identifies the element (or elements) that labels the current element.
|
|
23
|
+
* @see aria-labelledby https://www.w3.org/TR/wai-aria-1.1/#aria-labelledby
|
|
24
|
+
*/
|
|
25
|
+
'aria-labelledby'?: AriaAttributes['aria-label']
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* This function is called whenever the user hits "Escape" or clicks outside
|
|
29
|
+
* the dialog.
|
|
30
|
+
*/
|
|
31
|
+
onDismiss?: (event: MouseEvent | KeyboardEvent) => void
|
|
32
|
+
|
|
33
|
+
children: ReactElement[]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* This component is based on @reach/dialog.
|
|
38
|
+
* https://github.com/reach/reach-ui/blob/main/packages/dialog/src/index.tsx
|
|
39
|
+
* https://reach.tech/dialog
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
const DropdownMenu = ({
|
|
43
|
+
children,
|
|
44
|
+
testId = 'store-dropdown-menu',
|
|
45
|
+
style,
|
|
46
|
+
...otherProps
|
|
47
|
+
}: PropsWithChildren<DropdownMenuProps>) => {
|
|
48
|
+
const {
|
|
49
|
+
isOpen,
|
|
50
|
+
close,
|
|
51
|
+
onDismiss,
|
|
52
|
+
dropdownItemsRef,
|
|
53
|
+
selectedDropdownItemIndexRef,
|
|
54
|
+
id,
|
|
55
|
+
} = useDropdown()
|
|
56
|
+
|
|
57
|
+
const dropdownPosition = useDropdownPosition()
|
|
58
|
+
|
|
59
|
+
const childrenLength = children.length
|
|
60
|
+
|
|
61
|
+
const handleBackdropClick = (event: MouseEvent) => {
|
|
62
|
+
if (event.defaultPrevented) {
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
event.stopPropagation()
|
|
67
|
+
onDismiss?.()
|
|
68
|
+
close?.()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const handleDownPress = () => {
|
|
72
|
+
if (selectedDropdownItemIndexRef!.current < childrenLength - 1) {
|
|
73
|
+
selectedDropdownItemIndexRef!.current++
|
|
74
|
+
} else {
|
|
75
|
+
selectedDropdownItemIndexRef!.current = 0
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
dropdownItemsRef?.current[selectedDropdownItemIndexRef!.current]?.focus()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const handleUpPress = () => {
|
|
82
|
+
if (selectedDropdownItemIndexRef!.current > 0) {
|
|
83
|
+
selectedDropdownItemIndexRef!.current--
|
|
84
|
+
} else {
|
|
85
|
+
selectedDropdownItemIndexRef!.current = childrenLength - 1
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
dropdownItemsRef?.current[selectedDropdownItemIndexRef!.current]?.focus()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const handleHomePress = () => {
|
|
92
|
+
selectedDropdownItemIndexRef!.current = 0
|
|
93
|
+
dropdownItemsRef?.current[selectedDropdownItemIndexRef!.current]?.focus()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const handleEndPress = () => {
|
|
97
|
+
selectedDropdownItemIndexRef!.current = childrenLength - 1
|
|
98
|
+
dropdownItemsRef?.current[selectedDropdownItemIndexRef!.current]?.focus()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const handleEscapePress = () => {
|
|
102
|
+
onDismiss?.()
|
|
103
|
+
close?.()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const handleBackdropKeyDown = (event: KeyboardEvent) => {
|
|
107
|
+
if (event.defaultPrevented) {
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
event.key === 'Escape' && handleEscapePress()
|
|
112
|
+
|
|
113
|
+
event.key === 'ArrowDown' && handleDownPress()
|
|
114
|
+
|
|
115
|
+
event.key === 'ArrowUp' && handleUpPress()
|
|
116
|
+
|
|
117
|
+
event.key === 'Home' && handleHomePress()
|
|
118
|
+
|
|
119
|
+
event.key === 'End' && handleEndPress()
|
|
120
|
+
|
|
121
|
+
event.key === 'Tab' && event.preventDefault()
|
|
122
|
+
|
|
123
|
+
event.stopPropagation()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const clearChildrenReferences = () => {
|
|
127
|
+
dropdownItemsRef!.current = []
|
|
128
|
+
|
|
129
|
+
return null
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return isOpen
|
|
133
|
+
? createPortal(
|
|
134
|
+
<Overlay
|
|
135
|
+
data-store-dropdown-overlay
|
|
136
|
+
onClick={handleBackdropClick}
|
|
137
|
+
onKeyDown={handleBackdropKeyDown}
|
|
138
|
+
>
|
|
139
|
+
<div
|
|
140
|
+
role="menu"
|
|
141
|
+
aria-orientation="vertical"
|
|
142
|
+
data-store-dropdown-menu
|
|
143
|
+
data-testid={testId}
|
|
144
|
+
style={{ ...dropdownPosition, ...style }}
|
|
145
|
+
id={id}
|
|
146
|
+
{...otherProps}
|
|
147
|
+
>
|
|
148
|
+
{children}
|
|
149
|
+
</div>
|
|
150
|
+
</Overlay>,
|
|
151
|
+
document.body
|
|
152
|
+
)
|
|
153
|
+
: clearChildrenReferences()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export default DropdownMenu
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createContext } from 'react'
|
|
2
|
+
|
|
3
|
+
export type DropdownContextState = {
|
|
4
|
+
/**
|
|
5
|
+
* Control de Dropdown state as Opened (true) or Closed (false).
|
|
6
|
+
*/
|
|
7
|
+
isOpen: boolean
|
|
8
|
+
/**
|
|
9
|
+
* Reference to DropdownButton, used to calculate a position for the DropdownMenu.
|
|
10
|
+
*/
|
|
11
|
+
dropdownButtonRef: React.RefObject<HTMLButtonElement> | null
|
|
12
|
+
/**
|
|
13
|
+
* Reference to a selected DropdownItem, used to manipulate focus.
|
|
14
|
+
*/
|
|
15
|
+
selectedDropdownItemIndexRef: React.MutableRefObject<number> | null
|
|
16
|
+
/**
|
|
17
|
+
* Array of References to dropdownItems in a DropdownMenu.
|
|
18
|
+
*/
|
|
19
|
+
dropdownItemsRef: React.MutableRefObject<HTMLButtonElement[]> | null
|
|
20
|
+
/**
|
|
21
|
+
* Close DropdownMenu event inherited from Modal.
|
|
22
|
+
*/
|
|
23
|
+
onDismiss?(): void
|
|
24
|
+
/**
|
|
25
|
+
* Function responsible for close the DropdownMenu in this context.
|
|
26
|
+
*/
|
|
27
|
+
close?(): void
|
|
28
|
+
/**
|
|
29
|
+
* Function responsible for open the DropdownMenu in this context.
|
|
30
|
+
*/
|
|
31
|
+
open?(): void
|
|
32
|
+
/**
|
|
33
|
+
* Function responsible for switch the the DropdownMenu state in this context.
|
|
34
|
+
*/
|
|
35
|
+
toggle?(): void
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Identifier to be used in aria-controls
|
|
39
|
+
*/
|
|
40
|
+
id: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const defaultState: DropdownContextState = {
|
|
44
|
+
isOpen: false,
|
|
45
|
+
dropdownButtonRef: null,
|
|
46
|
+
selectedDropdownItemIndexRef: null,
|
|
47
|
+
dropdownItemsRef: null,
|
|
48
|
+
id: 'store-dropdown',
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const DropdownContext = createContext<DropdownContextState>(defaultState)
|
|
52
|
+
|
|
53
|
+
export default DropdownContext
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useContext } from 'react'
|
|
2
|
+
|
|
3
|
+
import type { DropdownContextState } from '../contexts/DropdownContext'
|
|
4
|
+
import DropdownContext from '../contexts/DropdownContext'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook to use the Dropdown context.
|
|
8
|
+
* @returns Dropdown context.
|
|
9
|
+
*/
|
|
10
|
+
export const useDropdown = () => {
|
|
11
|
+
const context = useContext<DropdownContextState>(DropdownContext)
|
|
12
|
+
|
|
13
|
+
if (context === undefined) {
|
|
14
|
+
throw new Error('Do not use useDropdown hook outside the Dropdown context.')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return context
|
|
18
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useDropdown } from './useDropdown'
|
|
2
|
+
|
|
3
|
+
type DropdownPosition = {
|
|
4
|
+
position: 'absolute'
|
|
5
|
+
top: number
|
|
6
|
+
left: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Hook used to find the DropdownMenu position in relation to DropdownButton
|
|
11
|
+
* @returns Style with positions.
|
|
12
|
+
*/
|
|
13
|
+
export const useDropdownPosition = (): DropdownPosition => {
|
|
14
|
+
const { dropdownButtonRef } = useDropdown()
|
|
15
|
+
|
|
16
|
+
const topLevel = dropdownButtonRef?.current?.offsetTop ?? 0
|
|
17
|
+
const topOffset = dropdownButtonRef?.current?.offsetHeight ?? 0
|
|
18
|
+
const topPosition = topLevel + topOffset
|
|
19
|
+
const leftPosition = dropdownButtonRef?.current?.offsetLeft ?? 0
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
position: 'absolute',
|
|
23
|
+
top: topPosition,
|
|
24
|
+
left: leftPosition,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { default } from './Dropdown'
|
|
2
|
+
export type { DropdownProps } from './Dropdown'
|
|
3
|
+
|
|
4
|
+
export { default as DropdownButton } from './DropdownButton'
|
|
5
|
+
export type { DropdownButtonProps } from './DropdownButton'
|
|
6
|
+
|
|
7
|
+
export { default as DropdownItem } from './DropdownItem'
|
|
8
|
+
export type { DropdownItemProps } from './DropdownItem'
|
|
9
|
+
|
|
10
|
+
export { default as DropdownMenu } from './DropdownMenu'
|
|
11
|
+
export type { DropdownMenuProps } from './DropdownMenu'
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Canvas, Props, Story, ArgsTable } from '@storybook/addon-docs'
|
|
2
|
+
|
|
3
|
+
import Dropdown from '../Dropdown'
|
|
4
|
+
import DropdownButton from '../DropdownButton'
|
|
5
|
+
import DropdownItem from '../DropdownItem'
|
|
6
|
+
import DropdownMenu from '../DropdownMenu'
|
|
7
|
+
|
|
8
|
+
# Dropdown
|
|
9
|
+
|
|
10
|
+
<Canvas>
|
|
11
|
+
<Story id="molecules-dropdown--uncontrolled" />
|
|
12
|
+
</Canvas>
|
|
13
|
+
|
|
14
|
+
## Components
|
|
15
|
+
|
|
16
|
+
The `Dropdown` uses the [Compound Component](https://kentcdodds.com/blog/compound-components-with-react-hooks) pattern, its components are:
|
|
17
|
+
|
|
18
|
+
- `Dropdown`: the wrapper component, can receive an `isOpen` and emit the event `onDismiss`, which can be used as a controlled component.
|
|
19
|
+
- `DropdownButton`: the button that emits the trigger to open de DropdownMenu.
|
|
20
|
+
- `DropdownItem`: an option on DropdownMenu.
|
|
21
|
+
- `DropdownMenu`: wraps a series of `DropdownItem`s.
|
|
22
|
+
|
|
23
|
+
## Props
|
|
24
|
+
|
|
25
|
+
### `Dropdown`
|
|
26
|
+
|
|
27
|
+
<ArgsTable of={Dropdown} />
|
|
28
|
+
|
|
29
|
+
### `DropdownButton`
|
|
30
|
+
|
|
31
|
+
<ArgsTable of={DropdownButton} />
|
|
32
|
+
|
|
33
|
+
### `DropdownItem`
|
|
34
|
+
|
|
35
|
+
<ArgsTable of={DropdownItem} />
|
|
36
|
+
|
|
37
|
+
### `DropdownMenu`
|
|
38
|
+
|
|
39
|
+
<ArgsTable of={DropdownMenu} />
|
|
40
|
+
|
|
41
|
+
## CSS Selectors
|
|
42
|
+
|
|
43
|
+
```css
|
|
44
|
+
[data-store-dropdown-button] {}
|
|
45
|
+
|
|
46
|
+
[data-store-dropdown-item] {}
|
|
47
|
+
|
|
48
|
+
[data-store-dropdown-overlay] {}
|
|
49
|
+
|
|
50
|
+
[data-store-dropdown-menu] {}
|
|
51
|
+
```
|