@khanacademy/wonder-blocks-accordion 3.1.71 → 3.2.0
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 +48 -0
- package/dist/components/accordion-section.d.ts +9 -2
- package/dist/components/accordion.d.ts +65 -40
- package/dist/es/index.js +2 -2
- package/dist/index.js +2 -2
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,53 @@
|
|
|
1
1
|
# @khanacademy/wonder-blocks-accordion
|
|
2
2
|
|
|
3
|
+
## 3.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 1f8807a: Accordion: add controlled mode via the `expandedIndices` and `onToggle` props, and fix the `id` prop so it is set on the accordion itself instead of being duplicated onto every list item
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [8ac496e]
|
|
12
|
+
- @khanacademy/wonder-blocks-clickable@8.3.0
|
|
13
|
+
|
|
14
|
+
## 3.1.72
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 8ce8b48: Warn when a heading is passed to `AccordionSection`'s `header` prop
|
|
19
|
+
|
|
20
|
+
`AccordionSection` wraps its trigger in a heading element containing a
|
|
21
|
+
`<button>`, and the `header` content renders inside that button. A heading in
|
|
22
|
+
there produces `h2 > button > h3`, which is invalid HTML — a `<button>` may only
|
|
23
|
+
contain phrasing content, and nested headings give screen reader users a
|
|
24
|
+
duplicated heading structure. Nothing caught this before.
|
|
25
|
+
|
|
26
|
+
- **eslint-plugin-wonder-blocks:** new `no-heading-in-accordion-header` rule,
|
|
27
|
+
enabled at `error` in the `recommended` config. It flags `<h1>`–`<h6>`, Wonder
|
|
28
|
+
Blocks `Heading` components, a `tag="h1"`–`tag="h6"` on any component (e.g.
|
|
29
|
+
`<BodyText tag="h1">`), and `role="heading"` anywhere inside a literal
|
|
30
|
+
`header` prop on `AccordionSection`. This is a major bump because projects
|
|
31
|
+
extending `recommended` will see new lint errors.
|
|
32
|
+
- **wonder-blocks-accordion:** `AccordionSection` now logs a development-only
|
|
33
|
+
console warning when the rendered header turns out to contain a heading. It
|
|
34
|
+
inspects the DOM, so it also catches headings rendered inside a consumer's own
|
|
35
|
+
components, which the lint rule cannot see. It does not fire during
|
|
36
|
+
server-side rendering. The `header` prop documentation now states the
|
|
37
|
+
constraint.
|
|
38
|
+
|
|
39
|
+
To set the heading level, use `AccordionSection`'s `tag` prop. For
|
|
40
|
+
heading-sized text without heading semantics, use `<BodyText tag="span">` with
|
|
41
|
+
the `font.heading.*` tokens.
|
|
42
|
+
|
|
43
|
+
- Updated dependencies [04522d4]
|
|
44
|
+
- Updated dependencies [5b4fed3]
|
|
45
|
+
- @khanacademy/wonder-blocks-tokens@18.0.0
|
|
46
|
+
- @khanacademy/wonder-blocks-clickable@8.2.11
|
|
47
|
+
- @khanacademy/wonder-blocks-icon@6.0.2
|
|
48
|
+
- @khanacademy/wonder-blocks-typography@5.0.5
|
|
49
|
+
- @khanacademy/wonder-blocks-core@12.5.0
|
|
50
|
+
|
|
3
51
|
## 3.1.71
|
|
4
52
|
|
|
5
53
|
### Patch Changes
|
|
@@ -59,8 +59,15 @@ declare const AccordionSection: React.ForwardRefExoticComponent<Readonly<import(
|
|
|
59
59
|
*/
|
|
60
60
|
children: string | React.ReactElement;
|
|
61
61
|
/**
|
|
62
|
-
* The header for this section.
|
|
63
|
-
*
|
|
62
|
+
* The header for this section. A string is given Heading typography and
|
|
63
|
+
* the standard header spacing; a React element is rendered as-is.
|
|
64
|
+
*
|
|
65
|
+
* NOTE: this content renders inside a `<button>` that is itself wrapped in
|
|
66
|
+
* a heading, so it must not contain a heading of its own (`<h1>`–`<h6>`,
|
|
67
|
+
* `Heading`, a `tag="h1"`–`tag="h6"` on any component, or
|
|
68
|
+
* `role="heading"`) — that is invalid HTML. Use AccordionSection's own
|
|
69
|
+
* `tag` prop to set the heading level, and `<BodyText tag="span">` with
|
|
70
|
+
* `font.heading.*` tokens for heading-sized text.
|
|
64
71
|
*/
|
|
65
72
|
header: string | React.ReactElement;
|
|
66
73
|
/**
|
|
@@ -1,53 +1,40 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import type { StyleType } from "@khanacademy/wonder-blocks-core";
|
|
2
|
+
import type { AriaProps, StyleType } from "@khanacademy/wonder-blocks-core";
|
|
3
3
|
import AccordionSection from "./accordion-section";
|
|
4
4
|
export type AccordionCornerKindType = "square" | "rounded" | "rounded-per-section";
|
|
5
|
-
|
|
6
|
-
* An accordion displays a vertically stacked list of sections, each of which
|
|
7
|
-
* contains content that can be shown or hidden by clicking its header.
|
|
8
|
-
*
|
|
9
|
-
* The Wonder Blocks Accordion component is a styled wrapper for a list of
|
|
10
|
-
* AccordionSection components. It also wraps the AccordionSection
|
|
11
|
-
* components in list items.
|
|
12
|
-
*
|
|
13
|
-
* ### Usage
|
|
14
|
-
*
|
|
15
|
-
* ```jsx
|
|
16
|
-
* import {
|
|
17
|
-
* Accordion,
|
|
18
|
-
* AccordionSection
|
|
19
|
-
* } from "@khanacademy/wonder-blocks-accordion";
|
|
20
|
-
*
|
|
21
|
-
* <Accordion>
|
|
22
|
-
* <AccordionSection header="First section">
|
|
23
|
-
* This is the information present in the first section
|
|
24
|
-
* </AccordionSection>
|
|
25
|
-
* <AccordionSection header="Second section">
|
|
26
|
-
* This is the information present in the second section
|
|
27
|
-
* </AccordionSection>
|
|
28
|
-
* <AccordionSection header="Third section">
|
|
29
|
-
* This is the information present in the third section
|
|
30
|
-
* </AccordionSection>
|
|
31
|
-
* </Accordion>
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
declare const Accordion: React.ForwardRefExoticComponent<Readonly<import("@khanacademy/wonder-blocks-core").AriaAttributes> & Readonly<{
|
|
35
|
-
role?: import("@khanacademy/wonder-blocks-core").AriaRole;
|
|
36
|
-
}> & {
|
|
5
|
+
type ControlledProps = {
|
|
37
6
|
/**
|
|
38
|
-
* The
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
*
|
|
7
|
+
* The indices of the AccordionSections that are currently expanded.
|
|
8
|
+
*
|
|
9
|
+
* Passing this prop puts the Accordion into controlled mode: the parent
|
|
10
|
+
* owns the expanded state, and the Accordion only reports changes via
|
|
11
|
+
* `onToggle`. Use `initialExpandedIndex` instead if you want the
|
|
12
|
+
* Accordion to manage the expanded state itself.
|
|
43
13
|
*/
|
|
44
|
-
|
|
14
|
+
expandedIndices: ReadonlyArray<number>;
|
|
15
|
+
initialExpandedIndex?: never;
|
|
16
|
+
};
|
|
17
|
+
type UncontrolledProps = {
|
|
45
18
|
/**
|
|
46
19
|
* The index of the AccordionSection that should be expanded when the
|
|
47
20
|
* Accordion is first rendered. If not specified, no AccordionSections
|
|
48
21
|
* will be expanded when the Accordion is first rendered.
|
|
22
|
+
*
|
|
23
|
+
* This is only read on the first render. Use `expandedIndices` if you
|
|
24
|
+
* need to control the expanded state after that.
|
|
49
25
|
*/
|
|
50
26
|
initialExpandedIndex?: number;
|
|
27
|
+
expandedIndices?: never;
|
|
28
|
+
};
|
|
29
|
+
type Props = AriaProps & {
|
|
30
|
+
/**
|
|
31
|
+
* The unique identifier for the accordion.
|
|
32
|
+
*/
|
|
33
|
+
id?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The AccordionSection components to display within this Accordion.
|
|
36
|
+
*/
|
|
37
|
+
children: Array<React.ReactElement<React.ComponentProps<typeof AccordionSection>>>;
|
|
51
38
|
/**
|
|
52
39
|
* Whether multiple AccordionSections can be expanded at the same time.
|
|
53
40
|
* If not specified, multiple AccordionSections can be expanded at a time.
|
|
@@ -91,5 +78,43 @@ declare const Accordion: React.ForwardRefExoticComponent<Readonly<import("@khana
|
|
|
91
78
|
* Custom styles for the overall accordion container.
|
|
92
79
|
*/
|
|
93
80
|
style?: StyleType;
|
|
94
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Called when a section is toggled. It is passed the index of the section
|
|
83
|
+
* that was toggled, along with the indices of every section that is
|
|
84
|
+
* expanded as a result of the toggle. In controlled mode, the second
|
|
85
|
+
* argument is the value that should be passed back in as
|
|
86
|
+
* `expandedIndices`.
|
|
87
|
+
*/
|
|
88
|
+
onToggle?: (toggledIndex: number, allExpandedIndices: Array<number>) => unknown;
|
|
89
|
+
} & (ControlledProps | UncontrolledProps);
|
|
90
|
+
/**
|
|
91
|
+
* An accordion displays a vertically stacked list of sections, each of which
|
|
92
|
+
* contains content that can be shown or hidden by clicking its header.
|
|
93
|
+
*
|
|
94
|
+
* The Wonder Blocks Accordion component is a styled wrapper for a list of
|
|
95
|
+
* AccordionSection components. It also wraps the AccordionSection
|
|
96
|
+
* components in list items.
|
|
97
|
+
*
|
|
98
|
+
* ### Usage
|
|
99
|
+
*
|
|
100
|
+
* ```jsx
|
|
101
|
+
* import {
|
|
102
|
+
* Accordion,
|
|
103
|
+
* AccordionSection
|
|
104
|
+
* } from "@khanacademy/wonder-blocks-accordion";
|
|
105
|
+
*
|
|
106
|
+
* <Accordion>
|
|
107
|
+
* <AccordionSection header="First section">
|
|
108
|
+
* This is the information present in the first section
|
|
109
|
+
* </AccordionSection>
|
|
110
|
+
* <AccordionSection header="Second section">
|
|
111
|
+
* This is the information present in the second section
|
|
112
|
+
* </AccordionSection>
|
|
113
|
+
* <AccordionSection header="Third section">
|
|
114
|
+
* This is the information present in the third section
|
|
115
|
+
* </AccordionSection>
|
|
116
|
+
* </Accordion>
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
declare const Accordion: React.ForwardRefExoticComponent<Props & React.RefAttributes<HTMLUListElement>>;
|
|
95
120
|
export default Accordion;
|
package/dist/es/index.js
CHANGED
|
@@ -9,11 +9,11 @@ import caretDown from '@phosphor-icons/core/bold/caret-down-bold.svg';
|
|
|
9
9
|
import Clickable from '@khanacademy/wonder-blocks-clickable';
|
|
10
10
|
import { PhosphorIcon } from '@khanacademy/wonder-blocks-icon';
|
|
11
11
|
|
|
12
|
-
const StyledUl=addStyle("ul");const LANDMARK_PROLIFERATION_THRESHOLD=6;const Accordion=React.forwardRef(function Accordion(props,ref){const{children,id,
|
|
12
|
+
const StyledUl=addStyle("ul");const LANDMARK_PROLIFERATION_THRESHOLD=6;const Accordion=React.forwardRef(function Accordion(props,ref){const{children,id,allowMultipleExpanded=true,caretPosition,cornerKind="rounded",animated,style,onToggle,expandedIndices,initialExpandedIndex,...ariaProps}=props;const isControlled=expandedIndices!==undefined;const[internalSectionsOpened,setInternalSectionsOpened]=React.useState(()=>{const startingArray=Array(children.length).fill(false);if(initialExpandedIndex!==undefined){startingArray[initialExpandedIndex]=true;}return startingArray});const controlledSectionsOpened=React.useMemo(()=>{if(expandedIndices===undefined){return null}const openedArray=Array(children.length).fill(false);for(const index of expandedIndices){openedArray[index]=true;}return openedArray},[children.length,expandedIndices]);const sectionsOpened=controlledSectionsOpened??internalSectionsOpened;const childRefs=Array(children.length).fill(null);const sectionsAreRegions=children.length<=LANDMARK_PROLIFERATION_THRESHOLD;const handleSectionClick=(index,childOnToggle)=>{const newSectionsOpened=allowMultipleExpanded?[...sectionsOpened]:Array(children.length).fill(false);const newOpenedValueAtIndex=!sectionsOpened[index];newSectionsOpened[index]=newOpenedValueAtIndex;if(!isControlled){setInternalSectionsOpened(newSectionsOpened);}childOnToggle?.(newOpenedValueAtIndex);onToggle?.(index,newSectionsOpened.flatMap((isOpened,sectionIndex)=>isOpened?[sectionIndex]:[]));};const handleKeyDown=event=>{const currentlyFocusedHeaderIndex=childRefs.findIndex(ref=>ref.current===document.activeElement);if(currentlyFocusedHeaderIndex===-1){return}switch(event.key){case"ArrowUp":event.preventDefault();const previousSectionIndex=(currentlyFocusedHeaderIndex+children.length-1)%children.length;const previousChildRef=childRefs[previousSectionIndex];previousChildRef.current?.focus();break;case"ArrowDown":event.preventDefault();const nextSectionIndex=(currentlyFocusedHeaderIndex+1)%children.length;const nextChildRef=childRefs[nextSectionIndex];nextChildRef.current?.focus();break;case"Home":event.preventDefault();const firstChildRef=childRefs[0];firstChildRef.current?.focus();break;case"End":event.preventDefault();const lastChildRef=childRefs[children.length-1];lastChildRef.current?.focus();break}};return jsx(StyledUl,{id:id,style:[styles$2.wrapper,style],onKeyDown:handleKeyDown,...ariaProps,ref:ref,children:children.map((child,index)=>{const{caretPosition:childCaretPosition,cornerKind:childCornerKind,onToggle:childOnToggle,animated:childAnimated}=child.props;const childRef=React.createRef();childRefs[index]=childRef;const isFirstChild=index===0;const isLastChild=index===children.length-1;return jsx("li",{id:id?`${id}-section-${index}`:undefined,children:React.cloneElement(child,{animated:childAnimated??animated,caretPosition:childCaretPosition??caretPosition,cornerKind:childCornerKind??cornerKind,expanded:sectionsOpened[index]??false,onToggle:()=>handleSectionClick(index,childOnToggle),isFirstSection:isFirstChild,isLastSection:isLastChild,isRegion:sectionsAreRegions,ref:childRef})},index)})})});const styles$2=StyleSheet.create({wrapper:{boxSizing:"border-box",listStyle:"none",padding:0,width:"100%"}});
|
|
13
13
|
|
|
14
14
|
function getRoundedValuesForHeader(cornerKind,isFirstSection,isLastSection,expanded){switch(cornerKind){case"rounded-per-section":return {roundedTop:true,roundedBottom:!expanded};case"rounded":return {roundedTop:isFirstSection,roundedBottom:isLastSection&&!expanded};default:return {roundedTop:false,roundedBottom:false}}}
|
|
15
15
|
|
|
16
|
-
const AccordionSectionHeader=React.forwardRef(function AccordionSectionHeader(props,ref){const{id,header,caretPosition,cornerKind,collapsible=true,expanded,animated,onClick,sectionContentUniqueId,headerStyle,tag="h2",testId,isFirstSection,isLastSection}=props;const headerIsString=typeof header==="string";const{roundedTop,roundedBottom}=getRoundedValuesForHeader(cornerKind,isFirstSection,isLastSection,expanded);return jsx(Heading,{size:"medium",tag:tag,style:styles$1.heading,children:jsx(Clickable,{id:id,"aria-expanded":expanded,"aria-controls":sectionContentUniqueId,onClick:onClick,disabled:!collapsible,testId:testId?`${testId}-header`:undefined,style:[styles$1.headerWrapper,animated&&styles$1.headerWrapperWithAnimation,caretPosition==="start"&&styles$1.headerWrapperCaretStart,roundedTop&&styles$1.roundedTop,roundedBottom&&styles$1.roundedBottom,headerStyle,!collapsible&&styles$1.disabled],ref:ref,children:()=>jsxs(Fragment,{children:[jsx(View,{style:[styles$1.headerContent,headerIsString&&styles$1.headerString],children:headerIsString?jsx(View,{style:[caretPosition==="end"?styles$1.headerStringCaretEnd:styles$1.headerStringCaretStart],children:header}):header}),collapsible&&jsx(PhosphorIcon,{icon:caretDown,color:semanticColor.core.foreground.neutral.default,size:"small",style:[animated&&styles$1.iconWithAnimation,caretPosition==="start"?styles$1.iconStart:styles$1.iconEnd,expanded&&styles$1.iconExpanded],testId:testId?`${testId}-caret-icon`:undefined})]})})})});const INNER_BORDER_RADIUS=`calc(${sizing.size_120} - 1px)`;const ANIMATION_LENGTH="300ms";const styles$1=StyleSheet.create({heading:{minInlineSize:0,marginBlockStart:0},headerWrapper:{display:"flex",flexDirection:"row",alignItems:"center",overflow:"hidden",minInlineSize:"auto",width:"100%",position:"relative",zIndex:1,":active":{outline:`2px solid ${semanticColor.action.secondary.progressive.press.border}`},":hover":{outline:`2px solid ${semanticColor.action.secondary.progressive.hover.border}`},":focus-visible":{outline:`2px solid ${semanticColor.focus.outer}`}},headerWrapperWithAnimation:{transition:`border-radius ${ANIMATION_LENGTH}`},headerWrapperCaretStart:{flexDirection:"row-reverse"},roundedTop:{borderStartStartRadius:INNER_BORDER_RADIUS,borderStartEndRadius:INNER_BORDER_RADIUS},roundedBottom:{borderEndStartRadius:INNER_BORDER_RADIUS,borderEndEndRadius:INNER_BORDER_RADIUS},headerContent:{flexGrow:1,textAlign:"start"},headerString:{paddingBlockStart:sizing.size_160,paddingBlockEnd:sizing.size_160},headerStringCaretEnd:{paddingInlineEnd:sizing.size_120,paddingInlineStart:sizing.size_160},headerStringCaretStart:{paddingInlineEnd:sizing.size_160,paddingInlineStart:sizing.size_120},iconWithAnimation:{transition:`transform ${ANIMATION_LENGTH}`},iconExpanded:{transform:"rotate(180deg)"},iconStart:{marginInlineStart:sizing.size_160},iconEnd:{marginInlineEnd:sizing.size_160},disabled:{pointerEvents:"none",color:"inherit",":focus-visible":{outline:`2px solid ${semanticColor.focus.outer}`}}});
|
|
16
|
+
const HEADING_SELECTOR='h1,h2,h3,h4,h5,h6,[role="heading"]';const AccordionSectionHeader=React.forwardRef(function AccordionSectionHeader(props,ref){const{id,header,caretPosition,cornerKind,collapsible=true,expanded,animated,onClick,sectionContentUniqueId,headerStyle,tag="h2",testId,isFirstSection,isLastSection}=props;const headerIsString=typeof header==="string";const headerContentRef=React.useRef(null);React.useEffect(()=>{if(process.env.NODE_ENV==="production"||headerIsString){return}if(headerContentRef.current?.querySelector(HEADING_SELECTOR)){console.warn("AccordionSection's header contains a heading. It is rendered "+"inside a <button> that AccordionSection already wraps in "+"a heading, which is invalid HTML. Set the heading level "+"with the `tag` prop instead, and use "+'`<BodyText tag="span">` with `font.heading.*` tokens for '+"heading-sized text.");}},[header,headerIsString]);const{roundedTop,roundedBottom}=getRoundedValuesForHeader(cornerKind,isFirstSection,isLastSection,expanded);return jsx(Heading,{size:"medium",tag:tag,style:styles$1.heading,children:jsx(Clickable,{id:id,"aria-expanded":expanded,"aria-controls":sectionContentUniqueId,onClick:onClick,disabled:!collapsible,testId:testId?`${testId}-header`:undefined,style:[styles$1.headerWrapper,animated&&styles$1.headerWrapperWithAnimation,caretPosition==="start"&&styles$1.headerWrapperCaretStart,roundedTop&&styles$1.roundedTop,roundedBottom&&styles$1.roundedBottom,headerStyle,!collapsible&&styles$1.disabled],ref:ref,children:()=>jsxs(Fragment,{children:[jsx(View,{style:[styles$1.headerContent,headerIsString&&styles$1.headerString],ref:headerContentRef,children:headerIsString?jsx(View,{style:[caretPosition==="end"?styles$1.headerStringCaretEnd:styles$1.headerStringCaretStart],children:header}):header}),collapsible&&jsx(PhosphorIcon,{icon:caretDown,color:semanticColor.core.foreground.neutral.default,size:"small",style:[animated&&styles$1.iconWithAnimation,caretPosition==="start"?styles$1.iconStart:styles$1.iconEnd,expanded&&styles$1.iconExpanded],testId:testId?`${testId}-caret-icon`:undefined})]})})})});const INNER_BORDER_RADIUS=`calc(${sizing.size_120} - 1px)`;const ANIMATION_LENGTH="300ms";const styles$1=StyleSheet.create({heading:{minInlineSize:0,marginBlockStart:0},headerWrapper:{display:"flex",flexDirection:"row",alignItems:"center",overflow:"hidden",minInlineSize:"auto",width:"100%",position:"relative",zIndex:1,":active":{outline:`2px solid ${semanticColor.action.secondary.progressive.press.border}`},":hover":{outline:`2px solid ${semanticColor.action.secondary.progressive.hover.border}`},":focus-visible":{outline:`2px solid ${semanticColor.focus.outer}`}},headerWrapperWithAnimation:{transition:`border-radius ${ANIMATION_LENGTH}`},headerWrapperCaretStart:{flexDirection:"row-reverse"},roundedTop:{borderStartStartRadius:INNER_BORDER_RADIUS,borderStartEndRadius:INNER_BORDER_RADIUS},roundedBottom:{borderEndStartRadius:INNER_BORDER_RADIUS,borderEndEndRadius:INNER_BORDER_RADIUS},headerContent:{flexGrow:1,textAlign:"start"},headerString:{paddingBlockStart:sizing.size_160,paddingBlockEnd:sizing.size_160},headerStringCaretEnd:{paddingInlineEnd:sizing.size_120,paddingInlineStart:sizing.size_160},headerStringCaretStart:{paddingInlineEnd:sizing.size_160,paddingInlineStart:sizing.size_120},iconWithAnimation:{transition:`transform ${ANIMATION_LENGTH}`},iconExpanded:{transform:"rotate(180deg)"},iconStart:{marginInlineStart:sizing.size_160},iconEnd:{marginInlineEnd:sizing.size_160},disabled:{pointerEvents:"none",color:"inherit",":focus-visible":{outline:`2px solid ${semanticColor.focus.outer}`}}});
|
|
17
17
|
|
|
18
18
|
const AccordionSection=React.forwardRef(function AccordionSection(props,ref){const{children,id,header,collapsible,expanded,animated=false,onToggle,caretPosition="end",cornerKind="rounded",style,headerStyle,tag,testId,isFirstSection=true,isLastSection=true,isRegion=true,...ariaProps}=props;const[internalExpanded,setInternalExpanded]=React.useState(expanded??false);const controlledMode=expanded!==undefined&&onToggle;const uniqueSectionId=useId();const sectionId=id??uniqueSectionId;const uniqueHeaderId=useId();const headerId=id?`${id}-header`:uniqueHeaderId;const sectionContentUniqueId=useId();const sectionStyles=_generateStyles(cornerKind,isFirstSection,isLastSection);const handleClick=()=>{if(controlledMode){onToggle(!expanded);}else {setInternalExpanded(!internalExpanded);if(onToggle){onToggle(!internalExpanded);}}};let expandedState;if(collapsible===false){expandedState=true;}else {expandedState=controlledMode?expanded:internalExpanded;}return jsxs(View,{id:sectionId,"data-expanded":expandedState?"true":"false",style:[styles.wrapper,animated&&styles.wrapperWithAnimation,sectionStyles.wrapper,style],testId:testId,...ariaProps,children:[jsx(AccordionSectionHeader,{id:headerId,header:header,caretPosition:caretPosition,cornerKind:cornerKind,collapsible:collapsible,expanded:expandedState,animated:animated,onClick:handleClick,sectionContentUniqueId:sectionContentUniqueId,headerStyle:headerStyle,tag:tag,testId:testId,isFirstSection:isFirstSection,isLastSection:isLastSection,ref:ref}),jsx(View,{id:sectionContentUniqueId,role:isRegion?"region":undefined,"aria-labelledby":headerId,style:[styles.contentWrapper,expandedState?styles.contentWrapperExpanded:styles.conentWrapperCollapsed,sectionStyles.contentWrapper],testId:testId?`${testId}-content-panel`:undefined,children:typeof children==="string"?jsx(BodyText,{tag:"span",style:styles.stringContent,children:children}):children})]})});const styles=StyleSheet.create({wrapper:{display:"grid",gridTemplateRows:"min-content 1fr",[':not([data-expanded="true"])']:{gridTemplateRows:"min-content 0fr"},position:"static",boxSizing:"border-box",backgroundColor:semanticColor.core.background.base.default},wrapperWithAnimation:{transition:"grid-template-rows 300ms"},contentWrapper:{overflow:"hidden"},conentWrapperCollapsed:{visibility:"hidden"},contentWrapperExpanded:{visibility:"visible"},stringContent:{padding:sizing.size_160}});const cornerStyles={};const _generateStyles=(cornerKind,isFirstSection,isLastSection)=>{const sectionType=`${cornerKind}-${isFirstSection.toString()}-${isLastSection.toString()}`;if(cornerStyles[sectionType]){return cornerStyles[sectionType]}let wrapperStyle=Object.freeze({});let contentWrapperStyle=Object.freeze({});let firstSectionStyle=Object.freeze({});let lastSectionStyle=Object.freeze({});const borderStyle=`1px solid ${semanticColor.core.border.neutral.subtle}`;if(cornerKind==="square"){wrapperStyle={border:borderStyle,borderBottom:"none",borderRadius:border.radius.radius_0};if(isLastSection){lastSectionStyle={borderBottom:borderStyle};}}if(cornerKind==="rounded"){wrapperStyle={border:borderStyle,borderBottom:"none"};if(isFirstSection){firstSectionStyle={borderStartStartRadius:sizing.size_120,borderStartEndRadius:sizing.size_120};}if(isLastSection){lastSectionStyle={borderBottom:borderStyle,borderEndStartRadius:sizing.size_120,borderEndEndRadius:sizing.size_120};contentWrapperStyle={borderEndEndRadius:sizing.size_120,borderEndStartRadius:sizing.size_120};}}if(cornerKind==="rounded-per-section"){wrapperStyle={border:borderStyle,borderRadius:border.radius.radius_120,marginBlockEnd:sizing.size_160};contentWrapperStyle={borderEndEndRadius:sizing.size_120,borderEndStartRadius:sizing.size_120};}const newStyles={wrapper:{...wrapperStyle,...firstSectionStyle,...lastSectionStyle},contentWrapper:contentWrapperStyle};cornerStyles[sectionType]=StyleSheet.create(newStyles);return cornerStyles[sectionType]};
|
|
19
19
|
|
package/dist/index.js
CHANGED
|
@@ -36,11 +36,11 @@ var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
|
36
36
|
var caretDown__default = /*#__PURE__*/_interopDefaultLegacy(caretDown);
|
|
37
37
|
var Clickable__default = /*#__PURE__*/_interopDefaultLegacy(Clickable);
|
|
38
38
|
|
|
39
|
-
const StyledUl=wonderBlocksCore.addStyle("ul");const LANDMARK_PROLIFERATION_THRESHOLD=6;const Accordion=React__namespace.forwardRef(function Accordion(props,ref){const{children,id,
|
|
39
|
+
const StyledUl=wonderBlocksCore.addStyle("ul");const LANDMARK_PROLIFERATION_THRESHOLD=6;const Accordion=React__namespace.forwardRef(function Accordion(props,ref){const{children,id,allowMultipleExpanded=true,caretPosition,cornerKind="rounded",animated,style,onToggle,expandedIndices,initialExpandedIndex,...ariaProps}=props;const isControlled=expandedIndices!==undefined;const[internalSectionsOpened,setInternalSectionsOpened]=React__namespace.useState(()=>{const startingArray=Array(children.length).fill(false);if(initialExpandedIndex!==undefined){startingArray[initialExpandedIndex]=true;}return startingArray});const controlledSectionsOpened=React__namespace.useMemo(()=>{if(expandedIndices===undefined){return null}const openedArray=Array(children.length).fill(false);for(const index of expandedIndices){openedArray[index]=true;}return openedArray},[children.length,expandedIndices]);const sectionsOpened=controlledSectionsOpened??internalSectionsOpened;const childRefs=Array(children.length).fill(null);const sectionsAreRegions=children.length<=LANDMARK_PROLIFERATION_THRESHOLD;const handleSectionClick=(index,childOnToggle)=>{const newSectionsOpened=allowMultipleExpanded?[...sectionsOpened]:Array(children.length).fill(false);const newOpenedValueAtIndex=!sectionsOpened[index];newSectionsOpened[index]=newOpenedValueAtIndex;if(!isControlled){setInternalSectionsOpened(newSectionsOpened);}childOnToggle?.(newOpenedValueAtIndex);onToggle?.(index,newSectionsOpened.flatMap((isOpened,sectionIndex)=>isOpened?[sectionIndex]:[]));};const handleKeyDown=event=>{const currentlyFocusedHeaderIndex=childRefs.findIndex(ref=>ref.current===document.activeElement);if(currentlyFocusedHeaderIndex===-1){return}switch(event.key){case"ArrowUp":event.preventDefault();const previousSectionIndex=(currentlyFocusedHeaderIndex+children.length-1)%children.length;const previousChildRef=childRefs[previousSectionIndex];previousChildRef.current?.focus();break;case"ArrowDown":event.preventDefault();const nextSectionIndex=(currentlyFocusedHeaderIndex+1)%children.length;const nextChildRef=childRefs[nextSectionIndex];nextChildRef.current?.focus();break;case"Home":event.preventDefault();const firstChildRef=childRefs[0];firstChildRef.current?.focus();break;case"End":event.preventDefault();const lastChildRef=childRefs[children.length-1];lastChildRef.current?.focus();break}};return jsxRuntime.jsx(StyledUl,{id:id,style:[styles$2.wrapper,style],onKeyDown:handleKeyDown,...ariaProps,ref:ref,children:children.map((child,index)=>{const{caretPosition:childCaretPosition,cornerKind:childCornerKind,onToggle:childOnToggle,animated:childAnimated}=child.props;const childRef=React__namespace.createRef();childRefs[index]=childRef;const isFirstChild=index===0;const isLastChild=index===children.length-1;return jsxRuntime.jsx("li",{id:id?`${id}-section-${index}`:undefined,children:React__namespace.cloneElement(child,{animated:childAnimated??animated,caretPosition:childCaretPosition??caretPosition,cornerKind:childCornerKind??cornerKind,expanded:sectionsOpened[index]??false,onToggle:()=>handleSectionClick(index,childOnToggle),isFirstSection:isFirstChild,isLastSection:isLastChild,isRegion:sectionsAreRegions,ref:childRef})},index)})})});const styles$2=aphrodite.StyleSheet.create({wrapper:{boxSizing:"border-box",listStyle:"none",padding:0,width:"100%"}});
|
|
40
40
|
|
|
41
41
|
function getRoundedValuesForHeader(cornerKind,isFirstSection,isLastSection,expanded){switch(cornerKind){case"rounded-per-section":return {roundedTop:true,roundedBottom:!expanded};case"rounded":return {roundedTop:isFirstSection,roundedBottom:isLastSection&&!expanded};default:return {roundedTop:false,roundedBottom:false}}}
|
|
42
42
|
|
|
43
|
-
const AccordionSectionHeader=React__namespace.forwardRef(function AccordionSectionHeader(props,ref){const{id,header,caretPosition,cornerKind,collapsible=true,expanded,animated,onClick,sectionContentUniqueId,headerStyle,tag="h2",testId,isFirstSection,isLastSection}=props;const headerIsString=typeof header==="string";const{roundedTop,roundedBottom}=getRoundedValuesForHeader(cornerKind,isFirstSection,isLastSection,expanded);return jsxRuntime.jsx(wonderBlocksTypography.Heading,{size:"medium",tag:tag,style:styles$1.heading,children:jsxRuntime.jsx(Clickable__default["default"],{id:id,"aria-expanded":expanded,"aria-controls":sectionContentUniqueId,onClick:onClick,disabled:!collapsible,testId:testId?`${testId}-header`:undefined,style:[styles$1.headerWrapper,animated&&styles$1.headerWrapperWithAnimation,caretPosition==="start"&&styles$1.headerWrapperCaretStart,roundedTop&&styles$1.roundedTop,roundedBottom&&styles$1.roundedBottom,headerStyle,!collapsible&&styles$1.disabled],ref:ref,children:()=>jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(wonderBlocksCore.View,{style:[styles$1.headerContent,headerIsString&&styles$1.headerString],children:headerIsString?jsxRuntime.jsx(wonderBlocksCore.View,{style:[caretPosition==="end"?styles$1.headerStringCaretEnd:styles$1.headerStringCaretStart],children:header}):header}),collapsible&&jsxRuntime.jsx(wonderBlocksIcon.PhosphorIcon,{icon:caretDown__default["default"],color:wonderBlocksTokens.semanticColor.core.foreground.neutral.default,size:"small",style:[animated&&styles$1.iconWithAnimation,caretPosition==="start"?styles$1.iconStart:styles$1.iconEnd,expanded&&styles$1.iconExpanded],testId:testId?`${testId}-caret-icon`:undefined})]})})})});const INNER_BORDER_RADIUS=`calc(${wonderBlocksTokens.sizing.size_120} - 1px)`;const ANIMATION_LENGTH="300ms";const styles$1=aphrodite.StyleSheet.create({heading:{minInlineSize:0,marginBlockStart:0},headerWrapper:{display:"flex",flexDirection:"row",alignItems:"center",overflow:"hidden",minInlineSize:"auto",width:"100%",position:"relative",zIndex:1,":active":{outline:`2px solid ${wonderBlocksTokens.semanticColor.action.secondary.progressive.press.border}`},":hover":{outline:`2px solid ${wonderBlocksTokens.semanticColor.action.secondary.progressive.hover.border}`},":focus-visible":{outline:`2px solid ${wonderBlocksTokens.semanticColor.focus.outer}`}},headerWrapperWithAnimation:{transition:`border-radius ${ANIMATION_LENGTH}`},headerWrapperCaretStart:{flexDirection:"row-reverse"},roundedTop:{borderStartStartRadius:INNER_BORDER_RADIUS,borderStartEndRadius:INNER_BORDER_RADIUS},roundedBottom:{borderEndStartRadius:INNER_BORDER_RADIUS,borderEndEndRadius:INNER_BORDER_RADIUS},headerContent:{flexGrow:1,textAlign:"start"},headerString:{paddingBlockStart:wonderBlocksTokens.sizing.size_160,paddingBlockEnd:wonderBlocksTokens.sizing.size_160},headerStringCaretEnd:{paddingInlineEnd:wonderBlocksTokens.sizing.size_120,paddingInlineStart:wonderBlocksTokens.sizing.size_160},headerStringCaretStart:{paddingInlineEnd:wonderBlocksTokens.sizing.size_160,paddingInlineStart:wonderBlocksTokens.sizing.size_120},iconWithAnimation:{transition:`transform ${ANIMATION_LENGTH}`},iconExpanded:{transform:"rotate(180deg)"},iconStart:{marginInlineStart:wonderBlocksTokens.sizing.size_160},iconEnd:{marginInlineEnd:wonderBlocksTokens.sizing.size_160},disabled:{pointerEvents:"none",color:"inherit",":focus-visible":{outline:`2px solid ${wonderBlocksTokens.semanticColor.focus.outer}`}}});
|
|
43
|
+
const HEADING_SELECTOR='h1,h2,h3,h4,h5,h6,[role="heading"]';const AccordionSectionHeader=React__namespace.forwardRef(function AccordionSectionHeader(props,ref){const{id,header,caretPosition,cornerKind,collapsible=true,expanded,animated,onClick,sectionContentUniqueId,headerStyle,tag="h2",testId,isFirstSection,isLastSection}=props;const headerIsString=typeof header==="string";const headerContentRef=React__namespace.useRef(null);React__namespace.useEffect(()=>{if(process.env.NODE_ENV==="production"||headerIsString){return}if(headerContentRef.current?.querySelector(HEADING_SELECTOR)){console.warn("AccordionSection's header contains a heading. It is rendered "+"inside a <button> that AccordionSection already wraps in "+"a heading, which is invalid HTML. Set the heading level "+"with the `tag` prop instead, and use "+'`<BodyText tag="span">` with `font.heading.*` tokens for '+"heading-sized text.");}},[header,headerIsString]);const{roundedTop,roundedBottom}=getRoundedValuesForHeader(cornerKind,isFirstSection,isLastSection,expanded);return jsxRuntime.jsx(wonderBlocksTypography.Heading,{size:"medium",tag:tag,style:styles$1.heading,children:jsxRuntime.jsx(Clickable__default["default"],{id:id,"aria-expanded":expanded,"aria-controls":sectionContentUniqueId,onClick:onClick,disabled:!collapsible,testId:testId?`${testId}-header`:undefined,style:[styles$1.headerWrapper,animated&&styles$1.headerWrapperWithAnimation,caretPosition==="start"&&styles$1.headerWrapperCaretStart,roundedTop&&styles$1.roundedTop,roundedBottom&&styles$1.roundedBottom,headerStyle,!collapsible&&styles$1.disabled],ref:ref,children:()=>jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[jsxRuntime.jsx(wonderBlocksCore.View,{style:[styles$1.headerContent,headerIsString&&styles$1.headerString],ref:headerContentRef,children:headerIsString?jsxRuntime.jsx(wonderBlocksCore.View,{style:[caretPosition==="end"?styles$1.headerStringCaretEnd:styles$1.headerStringCaretStart],children:header}):header}),collapsible&&jsxRuntime.jsx(wonderBlocksIcon.PhosphorIcon,{icon:caretDown__default["default"],color:wonderBlocksTokens.semanticColor.core.foreground.neutral.default,size:"small",style:[animated&&styles$1.iconWithAnimation,caretPosition==="start"?styles$1.iconStart:styles$1.iconEnd,expanded&&styles$1.iconExpanded],testId:testId?`${testId}-caret-icon`:undefined})]})})})});const INNER_BORDER_RADIUS=`calc(${wonderBlocksTokens.sizing.size_120} - 1px)`;const ANIMATION_LENGTH="300ms";const styles$1=aphrodite.StyleSheet.create({heading:{minInlineSize:0,marginBlockStart:0},headerWrapper:{display:"flex",flexDirection:"row",alignItems:"center",overflow:"hidden",minInlineSize:"auto",width:"100%",position:"relative",zIndex:1,":active":{outline:`2px solid ${wonderBlocksTokens.semanticColor.action.secondary.progressive.press.border}`},":hover":{outline:`2px solid ${wonderBlocksTokens.semanticColor.action.secondary.progressive.hover.border}`},":focus-visible":{outline:`2px solid ${wonderBlocksTokens.semanticColor.focus.outer}`}},headerWrapperWithAnimation:{transition:`border-radius ${ANIMATION_LENGTH}`},headerWrapperCaretStart:{flexDirection:"row-reverse"},roundedTop:{borderStartStartRadius:INNER_BORDER_RADIUS,borderStartEndRadius:INNER_BORDER_RADIUS},roundedBottom:{borderEndStartRadius:INNER_BORDER_RADIUS,borderEndEndRadius:INNER_BORDER_RADIUS},headerContent:{flexGrow:1,textAlign:"start"},headerString:{paddingBlockStart:wonderBlocksTokens.sizing.size_160,paddingBlockEnd:wonderBlocksTokens.sizing.size_160},headerStringCaretEnd:{paddingInlineEnd:wonderBlocksTokens.sizing.size_120,paddingInlineStart:wonderBlocksTokens.sizing.size_160},headerStringCaretStart:{paddingInlineEnd:wonderBlocksTokens.sizing.size_160,paddingInlineStart:wonderBlocksTokens.sizing.size_120},iconWithAnimation:{transition:`transform ${ANIMATION_LENGTH}`},iconExpanded:{transform:"rotate(180deg)"},iconStart:{marginInlineStart:wonderBlocksTokens.sizing.size_160},iconEnd:{marginInlineEnd:wonderBlocksTokens.sizing.size_160},disabled:{pointerEvents:"none",color:"inherit",":focus-visible":{outline:`2px solid ${wonderBlocksTokens.semanticColor.focus.outer}`}}});
|
|
44
44
|
|
|
45
45
|
const AccordionSection=React__namespace.forwardRef(function AccordionSection(props,ref){const{children,id,header,collapsible,expanded,animated=false,onToggle,caretPosition="end",cornerKind="rounded",style,headerStyle,tag,testId,isFirstSection=true,isLastSection=true,isRegion=true,...ariaProps}=props;const[internalExpanded,setInternalExpanded]=React__namespace.useState(expanded??false);const controlledMode=expanded!==undefined&&onToggle;const uniqueSectionId=React.useId();const sectionId=id??uniqueSectionId;const uniqueHeaderId=React.useId();const headerId=id?`${id}-header`:uniqueHeaderId;const sectionContentUniqueId=React.useId();const sectionStyles=_generateStyles(cornerKind,isFirstSection,isLastSection);const handleClick=()=>{if(controlledMode){onToggle(!expanded);}else {setInternalExpanded(!internalExpanded);if(onToggle){onToggle(!internalExpanded);}}};let expandedState;if(collapsible===false){expandedState=true;}else {expandedState=controlledMode?expanded:internalExpanded;}return jsxRuntime.jsxs(wonderBlocksCore.View,{id:sectionId,"data-expanded":expandedState?"true":"false",style:[styles.wrapper,animated&&styles.wrapperWithAnimation,sectionStyles.wrapper,style],testId:testId,...ariaProps,children:[jsxRuntime.jsx(AccordionSectionHeader,{id:headerId,header:header,caretPosition:caretPosition,cornerKind:cornerKind,collapsible:collapsible,expanded:expandedState,animated:animated,onClick:handleClick,sectionContentUniqueId:sectionContentUniqueId,headerStyle:headerStyle,tag:tag,testId:testId,isFirstSection:isFirstSection,isLastSection:isLastSection,ref:ref}),jsxRuntime.jsx(wonderBlocksCore.View,{id:sectionContentUniqueId,role:isRegion?"region":undefined,"aria-labelledby":headerId,style:[styles.contentWrapper,expandedState?styles.contentWrapperExpanded:styles.conentWrapperCollapsed,sectionStyles.contentWrapper],testId:testId?`${testId}-content-panel`:undefined,children:typeof children==="string"?jsxRuntime.jsx(wonderBlocksTypography.BodyText,{tag:"span",style:styles.stringContent,children:children}):children})]})});const styles=aphrodite.StyleSheet.create({wrapper:{display:"grid",gridTemplateRows:"min-content 1fr",[':not([data-expanded="true"])']:{gridTemplateRows:"min-content 0fr"},position:"static",boxSizing:"border-box",backgroundColor:wonderBlocksTokens.semanticColor.core.background.base.default},wrapperWithAnimation:{transition:"grid-template-rows 300ms"},contentWrapper:{overflow:"hidden"},conentWrapperCollapsed:{visibility:"hidden"},contentWrapperExpanded:{visibility:"visible"},stringContent:{padding:wonderBlocksTokens.sizing.size_160}});const cornerStyles={};const _generateStyles=(cornerKind,isFirstSection,isLastSection)=>{const sectionType=`${cornerKind}-${isFirstSection.toString()}-${isLastSection.toString()}`;if(cornerStyles[sectionType]){return cornerStyles[sectionType]}let wrapperStyle=Object.freeze({});let contentWrapperStyle=Object.freeze({});let firstSectionStyle=Object.freeze({});let lastSectionStyle=Object.freeze({});const borderStyle=`1px solid ${wonderBlocksTokens.semanticColor.core.border.neutral.subtle}`;if(cornerKind==="square"){wrapperStyle={border:borderStyle,borderBottom:"none",borderRadius:wonderBlocksTokens.border.radius.radius_0};if(isLastSection){lastSectionStyle={borderBottom:borderStyle};}}if(cornerKind==="rounded"){wrapperStyle={border:borderStyle,borderBottom:"none"};if(isFirstSection){firstSectionStyle={borderStartStartRadius:wonderBlocksTokens.sizing.size_120,borderStartEndRadius:wonderBlocksTokens.sizing.size_120};}if(isLastSection){lastSectionStyle={borderBottom:borderStyle,borderEndStartRadius:wonderBlocksTokens.sizing.size_120,borderEndEndRadius:wonderBlocksTokens.sizing.size_120};contentWrapperStyle={borderEndEndRadius:wonderBlocksTokens.sizing.size_120,borderEndStartRadius:wonderBlocksTokens.sizing.size_120};}}if(cornerKind==="rounded-per-section"){wrapperStyle={border:borderStyle,borderRadius:wonderBlocksTokens.border.radius.radius_120,marginBlockEnd:wonderBlocksTokens.sizing.size_160};contentWrapperStyle={borderEndEndRadius:wonderBlocksTokens.sizing.size_120,borderEndStartRadius:wonderBlocksTokens.sizing.size_120};}const newStyles={wrapper:{...wrapperStyle,...firstSectionStyle,...lastSectionStyle},contentWrapper:contentWrapperStyle};cornerStyles[sectionType]=aphrodite.StyleSheet.create(newStyles);return cornerStyles[sectionType]};
|
|
46
46
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Accordion components for Wonder Blocks.",
|
|
4
4
|
"author": "Khan Academy",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "3.
|
|
6
|
+
"version": "3.2.0",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"types": "dist/index.d.ts",
|
|
22
22
|
"source": "src/index.js",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@khanacademy/wonder-blocks-clickable": "8.
|
|
24
|
+
"@khanacademy/wonder-blocks-clickable": "8.3.0",
|
|
25
25
|
"@khanacademy/wonder-blocks-core": "12.5.0",
|
|
26
|
-
"@khanacademy/wonder-blocks-icon": "6.0.
|
|
27
|
-
"@khanacademy/wonder-blocks-tokens": "
|
|
28
|
-
"@khanacademy/wonder-blocks-typography": "5.0.
|
|
26
|
+
"@khanacademy/wonder-blocks-icon": "6.0.2",
|
|
27
|
+
"@khanacademy/wonder-blocks-tokens": "18.0.0",
|
|
28
|
+
"@khanacademy/wonder-blocks-typography": "5.0.5"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@phosphor-icons/core": "^2.0.2",
|