-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
193 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import ListItem from '../ListItem'; | ||
import { styled } from '../stitches.config'; | ||
|
||
const MenuItem = styled(ListItem, { | ||
'position': 'relative', | ||
'::after': { | ||
position: 'absolute', | ||
left: 0, | ||
top: '$1', | ||
bottom: '$1', | ||
width: '2px', | ||
content: `''`, | ||
bc: 'transparent', | ||
}, | ||
'variants': { | ||
'aria-selected': { | ||
true: { | ||
'bc': '$surfaceActive', | ||
'::after': { | ||
bc: '$primaryActive', | ||
}, | ||
}, | ||
false: { | ||
'::after': { | ||
bc: 'transparent', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
type Props = React.ComponentProps<typeof MenuItem> & { | ||
selected?: boolean; | ||
}; | ||
|
||
const Item: React.FC<Props> = ({ selected, ...props }) => { | ||
return <MenuItem {...props} role="treeitem" aria-selected={selected} tabIndex={selected ? 0 : -1} />; | ||
}; | ||
|
||
export default Item; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { useCallback, useRef } from 'react'; | ||
import { getFocusableTreeWalker } from '@react-aria/focus'; | ||
import Item from './Item'; | ||
import { useAllHandlers, useKeyboardHandles } from '../utils'; | ||
import { styled } from '../stitches.config'; | ||
import Section from './Section'; | ||
|
||
const List = styled('ul', { | ||
m: 0, | ||
p: 0, | ||
$outline: -1, | ||
}); | ||
|
||
type Props = React.ComponentProps<typeof List> & { | ||
label?: string; | ||
}; | ||
|
||
const MenuList: React.FC<Props> & { Item: typeof Item } = ({ label, ...props }) => { | ||
const ref = useRef<HTMLUListElement>(null); | ||
|
||
const focusElement = useCallback((fn: (walker: TreeWalker) => Node | null) => { | ||
let focusedElement = document.activeElement as HTMLElement; | ||
if (!ref.current?.contains(focusedElement)) { | ||
return; | ||
} | ||
|
||
// Create a DOM tree walker that matches all tabbable elements | ||
let walker = getFocusableTreeWalker(document.body); | ||
|
||
// Find the next tabbable element after the currently focused element | ||
walker.currentNode = focusedElement; | ||
let nextElement = fn(walker) as HTMLElement; | ||
if (ref.current.contains(nextElement)) { | ||
nextElement.focus(); | ||
} | ||
}, []); | ||
|
||
const handleNavigation = useKeyboardHandles({ | ||
ArrowUp: () => focusElement((walker) => walker.previousNode()), | ||
ArrowDown: () => focusElement((walker) => walker.nextNode()), | ||
}); | ||
const handleHeyDown = useAllHandlers(props.onKeyDown, handleNavigation); | ||
|
||
const list = <List {...props} onKeyDown={handleHeyDown} ref={ref} />; | ||
|
||
if (label) { | ||
return <Section label={label}>{list}</Section>; | ||
} | ||
return list; | ||
}; | ||
|
||
MenuList.Item = Item; | ||
|
||
export default MenuList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import { useUID } from 'react-uid'; | ||
import Flex from '../Flex'; | ||
import { styled } from '../stitches.config'; | ||
import Text from '../Text'; | ||
import MenuList from './index'; | ||
|
||
const Heading = styled(Text, { | ||
pr: '$2', | ||
textTransform: 'uppercase', | ||
}); | ||
|
||
const Section: React.FC<{ label: string; children: React.ReactElement<typeof MenuList> }> = ({ label, children }) => { | ||
const id = useUID(); | ||
return ( | ||
<Flex flow="column" space="xs"> | ||
<Heading id={id} font="mono" size="xs" tone="light"> | ||
{label} | ||
</Heading> | ||
{React.cloneElement(children, { 'aria-labelledby': id })} | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default Section; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './MenuList'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
name: MenuList | ||
--- | ||
|
||
import { Playground } from 'dokz'; | ||
import { Box, MenuList, ListItem, Icon, Text, Label } from '@fxtrot/ui'; | ||
import { HiOutlineUsers, HiOutlineAdjustments, HiOutlineGlobe } from 'react-icons/hi'; | ||
|
||
## MenuList | ||
|
||
<Playground> | ||
{() => { | ||
const [selected, setSelected] = React.useState('Appearance'); | ||
return ( | ||
<Box width="$56" p="$2"> | ||
<MenuList label="Settings" role="navigation"> | ||
{[ | ||
{ | ||
icon: HiOutlineUsers, | ||
label: 'Profiles', | ||
}, | ||
{ | ||
icon: HiOutlineAdjustments, | ||
label: 'Appearance', | ||
}, | ||
{ | ||
icon: HiOutlineGlobe, | ||
label: 'Language', | ||
}, | ||
].map(({ icon, label }) => ( | ||
<MenuList.Item key={label} selected={selected === label} onClick={() => setSelected(label)}> | ||
<Icon as={icon} /> | ||
<Text>{label}</Text> | ||
</MenuList.Item> | ||
))} | ||
</MenuList> | ||
</Box> | ||
); | ||
}} | ||
</Playground> |
afd2cd0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: