-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(accordion): ✨ add accordion components
- Loading branch information
1 parent
11c1346
commit 1b4b61e
Showing
10 changed files
with
870 additions
and
320 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React from "react"; | ||
import { Meta } from "@storybook/react"; | ||
|
||
import { useAccordionState } from "./AccordionState"; | ||
import { Accordion } from "./Accordion"; | ||
import { AccordionItem } from "./AccordionItem"; | ||
import { AccordionTrigger } from "./AccordionTrigger"; | ||
import { AccordionPanel } from "./AccordionPanel"; | ||
|
||
export default { | ||
title: "Component/Accordion", | ||
} as Meta; | ||
|
||
export const Default = () => { | ||
const state = useAccordionState(); | ||
|
||
return ( | ||
<Accordion {...state}> | ||
<AccordionItem {...state}> | ||
<h3> | ||
<AccordionTrigger {...state}>Trigger 1</AccordionTrigger> | ||
</h3> | ||
<AccordionPanel {...state}>Panel 1</AccordionPanel> | ||
</AccordionItem> | ||
<AccordionItem {...state}> | ||
<h3> | ||
<AccordionTrigger {...state}>Trigger 2</AccordionTrigger> | ||
</h3> | ||
<AccordionPanel {...state}>Panel 2</AccordionPanel> | ||
</AccordionItem> | ||
<AccordionItem {...state}> | ||
<h3> | ||
<AccordionTrigger {...state}>Trigger 3</AccordionTrigger> | ||
</h3> | ||
<AccordionPanel {...state}>Panel 4</AccordionPanel> | ||
</AccordionItem> | ||
</Accordion> | ||
); | ||
}; | ||
|
||
export const AllowMultiple = () => { | ||
const state = useAccordionState({ allowMultiple: true }); | ||
|
||
return ( | ||
<Accordion {...state}> | ||
<AccordionItem {...state}> | ||
<h3> | ||
<AccordionTrigger {...state}>Trigger 1</AccordionTrigger> | ||
</h3> | ||
<AccordionPanel {...state}>Panel 1</AccordionPanel> | ||
</AccordionItem> | ||
<AccordionItem {...state}> | ||
<h3> | ||
<AccordionTrigger {...state}>Trigger 2</AccordionTrigger> | ||
</h3> | ||
<AccordionPanel {...state}>Panel 2</AccordionPanel> | ||
</AccordionItem> | ||
<AccordionItem {...state}> | ||
<h3> | ||
<AccordionTrigger {...state}>Trigger 3</AccordionTrigger> | ||
</h3> | ||
<AccordionPanel {...state}>Panel 4</AccordionPanel> | ||
</AccordionItem> | ||
</Accordion> | ||
); | ||
}; |
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,13 @@ | ||
import { createComponent, createHook } from "reakit-system"; | ||
import { ACCORDION_KEYS } from "./__keys"; | ||
|
||
export const useAccordion = createHook<{}, {}>({ | ||
name: "AccordionPanel", | ||
keys: ACCORDION_KEYS, | ||
}); | ||
|
||
export const Accordion = createComponent({ | ||
as: "div", | ||
memo: true, | ||
useHook: useAccordion, | ||
}); |
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,49 @@ | ||
import * as React from "react"; | ||
import { createComponent, createHook } from "reakit-system"; | ||
import { | ||
unstable_IdHTMLProps, | ||
unstable_IdOptions, | ||
unstable_useId, | ||
} from "reakit"; | ||
import { useForkRef } from "reakit-utils"; | ||
|
||
import { AccordionStateReturn } from "./AccordionState"; | ||
import { ACCORDION_KEYS } from "./__keys"; | ||
|
||
export type AccordionItemOptions = unstable_IdOptions & AccordionStateReturn; | ||
|
||
export type AccordionItemHTMLProps = unstable_IdHTMLProps; | ||
|
||
export const useAccordionItem = createHook< | ||
AccordionItemOptions, | ||
AccordionItemHTMLProps | ||
>({ | ||
name: "AccordionItem", | ||
compose: [unstable_useId], | ||
keys: ACCORDION_KEYS, | ||
|
||
useProps(options, { ref: htmlRef, ...htmlProps }) { | ||
const ref = React.useRef<HTMLElement>(null); | ||
const { id } = options; | ||
|
||
React.useLayoutEffect(() => { | ||
if (!id) return undefined; | ||
|
||
options.registerItem?.({ id, ref }); | ||
|
||
/* eslint-disable-next-line react-hooks/exhaustive-deps */ | ||
}, [id]); | ||
|
||
return { ref: useForkRef(ref, htmlRef), ...htmlProps }; | ||
}, | ||
|
||
useComposeProps(_, htmlProps) { | ||
return { ...htmlProps }; | ||
}, | ||
}); | ||
|
||
export const AccordionItem = createComponent({ | ||
as: "div", | ||
memo: true, | ||
useHook: useAccordionItem, | ||
}); |
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,60 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
/* eslint-disable react-hooks/exhaustive-deps */ | ||
|
||
import * as React from "react"; | ||
import { createComponent, createHook } from "reakit-system"; | ||
import { | ||
unstable_IdHTMLProps, | ||
unstable_IdOptions, | ||
unstable_useId, | ||
} from "reakit"; | ||
import { useForkRef } from "reakit-utils"; | ||
|
||
import { AccordionStateReturn } from "./AccordionState"; | ||
import { ACCORDION_KEYS } from "./__keys"; | ||
|
||
export type AccordionPanelOptions = unstable_IdOptions & AccordionStateReturn; | ||
|
||
export type AccordionPanelHTMLProps = unstable_IdHTMLProps; | ||
|
||
export const useAccordionPanel = createHook< | ||
AccordionPanelOptions, | ||
AccordionPanelHTMLProps | ||
>({ | ||
name: "AccordionPanel", | ||
keys: ACCORDION_KEYS, | ||
compose: [unstable_useId], | ||
|
||
useProps(options, { ref: htmlRef, style: htmlStyle, ...htmlProps }) { | ||
const ref = React.useRef<HTMLElement>(null); | ||
|
||
const { id } = options; | ||
const item = options.items.find(({ panel }) => panel?.id === id); | ||
const buttonId = item?.button?.id; | ||
const isOpen = item ? options.activeItems.includes(item.id) : false; | ||
|
||
React.useEffect(() => { | ||
if (!id) return undefined; | ||
|
||
options.registerPanel?.({ id, ref }); | ||
}, [id]); | ||
|
||
const style = { | ||
display: `${isOpen ? "block" : "none"}`, | ||
...htmlStyle, | ||
}; | ||
|
||
return { | ||
ref: useForkRef(ref, htmlRef), | ||
role: "region", | ||
"aria-labelledby": `${buttonId ? buttonId : undefined}`, | ||
style, | ||
...htmlProps, | ||
}; | ||
}, | ||
}); | ||
|
||
export const AccordionPanel = createComponent({ | ||
as: "div", | ||
useHook: useAccordionPanel, | ||
}); |
Oops, something went wrong.