-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add accordion component and fix tab stops in git history
- Loading branch information
Showing
8 changed files
with
467 additions
and
127 deletions.
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
react-common/components/controls/Accordion/Accordion.tsx
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,155 @@ | ||
import * as React from "react"; | ||
import { ContainerProps, classList, fireClickOnEnter } from "../../util"; | ||
import { useId } from "../../../hooks/useId"; | ||
import { AccordionProvider, clearExpanded, setExpanded, useAccordionDispatch, useAccordionState } from "./context"; | ||
|
||
export interface AccordionProps extends ContainerProps { | ||
children?: React.ReactElement<AccordionItemProps>[] | React.ReactElement<AccordionItemProps>; | ||
} | ||
|
||
export interface AccordionItemProps extends ContainerProps { | ||
children?: [React.ReactElement<AccordionHeaderProps>, React.ReactElement<AccordionPanelProps>]; | ||
noChevron?: boolean; | ||
} | ||
|
||
export interface AccordionHeaderProps extends ContainerProps { | ||
} | ||
|
||
export interface AccordionPanelProps extends ContainerProps { | ||
} | ||
|
||
export const Accordion = (props: AccordionProps) => { | ||
const { | ||
children, | ||
id, | ||
className, | ||
ariaLabel, | ||
ariaHidden, | ||
ariaDescribedBy, | ||
role, | ||
} = props; | ||
|
||
|
||
return ( | ||
<AccordionProvider> | ||
<div | ||
className={classList("common-accordion", className)} | ||
id={id} | ||
aria-label={ariaLabel} | ||
aria-hidden={ariaHidden} | ||
aria-describedby={ariaDescribedBy} | ||
role={role} | ||
> | ||
{children} | ||
</div> | ||
</AccordionProvider> | ||
); | ||
}; | ||
|
||
export const AccordionItem = (props: AccordionItemProps) => { | ||
const { | ||
children, | ||
id, | ||
className, | ||
ariaLabel, | ||
ariaHidden, | ||
ariaDescribedBy, | ||
role, | ||
noChevron | ||
} = props; | ||
|
||
const { expanded } = useAccordionState(); | ||
const dispatch = useAccordionDispatch(); | ||
|
||
const panelId = useId(); | ||
const mappedChildren = React.Children.toArray(children); | ||
const isExpanded = expanded === panelId; | ||
|
||
const onHeaderClick = React.useCallback(() => { | ||
if (isExpanded) { | ||
dispatch(clearExpanded()); | ||
} | ||
else { | ||
dispatch(setExpanded(panelId)); | ||
} | ||
}, [isExpanded]); | ||
|
||
|
||
return ( | ||
<div | ||
className={classList("common-accordion", className)} | ||
id={id} | ||
aria-label={ariaLabel} | ||
aria-hidden={ariaHidden} | ||
aria-describedby={ariaDescribedBy} | ||
role={role} | ||
> | ||
<button | ||
className="common-accordion-header-outer" | ||
aria-expanded={isExpanded} | ||
aria-controls={panelId} | ||
onClick={onHeaderClick} | ||
onKeyDown={fireClickOnEnter} | ||
> | ||
<div> | ||
{!noChevron && | ||
<div className="common-accordion-chevron"> | ||
<i | ||
className={classList("fas", isExpanded ? "fa-chevron-down" : "fa-chevron-right")} | ||
aria-hidden={true} | ||
/> | ||
</div> | ||
} | ||
<div className="common-accordion-header-content"> | ||
{mappedChildren[0]} | ||
</div> | ||
</div> | ||
</button> | ||
<div | ||
id={panelId} | ||
className="common-accordion-panel-outer" | ||
style={{ display: expanded ? "block" : "none" }} | ||
> | ||
{isExpanded && mappedChildren[1]} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export const AccordionHeader = (props: AccordionHeaderProps) => { | ||
const { | ||
id, | ||
className, | ||
ariaLabel, | ||
children, | ||
} = props; | ||
|
||
return ( | ||
<div | ||
id={id} | ||
className={classList("common-accordion-header", className)} | ||
aria-label={ariaLabel} | ||
> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
export const AccordionPanel = (props: AccordionPanelProps) => { | ||
const { | ||
id, | ||
className, | ||
ariaLabel, | ||
children, | ||
} = props; | ||
|
||
return ( | ||
<div | ||
id={id} | ||
className={classList("common-accordion-body", className)} | ||
aria-label={ariaLabel} | ||
> | ||
{children} | ||
</div> | ||
); | ||
} |
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,70 @@ | ||
import * as React from "react"; | ||
|
||
interface AccordionState { | ||
expanded?: string; | ||
} | ||
|
||
const AccordionStateContext = React.createContext<AccordionState>(null); | ||
const AccordionDispatchContext = React.createContext<(action: Action) => void>(null); | ||
|
||
export const AccordionProvider = ({ children }: React.PropsWithChildren<{}>) => { | ||
const [state, dispatch] = React.useReducer( | ||
accordionReducer, | ||
{} | ||
); | ||
|
||
return ( | ||
<AccordionStateContext.Provider value={state}> | ||
<AccordionDispatchContext.Provider value={dispatch}> | ||
{children} | ||
</AccordionDispatchContext.Provider> | ||
</AccordionStateContext.Provider> | ||
) | ||
} | ||
|
||
type SetExpanded = { | ||
type: "SET_EXPANDED"; | ||
id: string; | ||
}; | ||
|
||
type ClearExpanded = { | ||
type: "CLEAR_EXPANDED"; | ||
}; | ||
|
||
type Action = SetExpanded | ClearExpanded; | ||
|
||
export const setExpanded = (id: string): SetExpanded => ( | ||
{ | ||
type: "SET_EXPANDED", | ||
id | ||
} | ||
); | ||
|
||
export const clearExpanded = (): ClearExpanded => ( | ||
{ | ||
type: "CLEAR_EXPANDED" | ||
} | ||
); | ||
|
||
export function useAccordionState() { | ||
return React.useContext(AccordionStateContext) | ||
} | ||
|
||
export function useAccordionDispatch() { | ||
return React.useContext(AccordionDispatchContext); | ||
} | ||
|
||
function accordionReducer(state: AccordionState, action: Action): AccordionState { | ||
switch (action.type) { | ||
case "SET_EXPANDED": | ||
return { | ||
...state, | ||
expanded: action.id | ||
}; | ||
case "CLEAR_EXPANDED": | ||
return { | ||
...state, | ||
expanded: undefined | ||
}; | ||
} | ||
} |
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 * from "./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,5 @@ | ||
import * as React from "react"; | ||
|
||
export function useId(): string { | ||
return React.useMemo(() => pxt.Util.guidGen(), []); | ||
} |
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,32 @@ | ||
.common-accordion-header-outer { | ||
cursor: pointer; | ||
background: none; | ||
color: inherit; | ||
border: none; | ||
padding: 0; | ||
font: inherit; | ||
outline: inherit; | ||
text-align: inherit; | ||
width: 100%; | ||
|
||
& > div { | ||
display: flex; | ||
flex-direction: row; | ||
width: 100%; | ||
} | ||
|
||
.common-accordion-chevron { | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
width: 2rem; | ||
} | ||
|
||
.common-accordion-header-content { | ||
flex-grow: 1; | ||
} | ||
} | ||
|
||
.common-accordion-header-outer:focus-visible { | ||
outline: @buttonFocusOutlineLightBackground; | ||
} |
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
Oops, something went wrong.