Collapsible
Collapsible is an element that allows a user to toggle (view/hide) supplemental information or actions.
When to use:
- To organize related information.
- To shorten pages and reduce scrolling when content is not crucial to read in full.
- When space is at a premium and long content cannot be displayed all at once.
Basic Usage
The styling
prop at the top level <Collapsible />
component determines if the collapsible has basic styling, card, or card with heading.
Basic Style
Any Paragon component or export may be added to the code example.
<Collapsiblestyling="basic"title="Toggle Collapsible"><p>Your stuff goes here.</p></Collapsible>
Card Style
This is the default style if no styling
prop is supplied.
Any Paragon component or export may be added to the code example.
() => {const [styling, setStyling] = useState('card');const [withIcon, setWithIcon] = useState(false);const iconProps = {iconWhenOpen: <span>CLOSE SESAME</span>,iconWhenClosed: <span>OPEN SESAME</span>,};return (<>{/* start example form block */}<ExamplePropsForminputs={[{ value: styling, setValue: setStyling, options: ['card', 'card-lg'], name: 'styling' },{ value: withIcon, setValue: () => setWithIcon(!withIcon), name: 'with icon' },]}/>{/* end example form block */}<Collapsiblestyling={styling}title={<p><strong>Toggle Collapsible</strong></p>}{...withIcon ? iconProps : {}}><p>Your stuff goes here.</p></Collapsible></>);}
Default Open
Any Paragon component or export may be added to the code example.
<Collapsible title="I'm not a heading" defaultOpen><p>Your stuff goes here.</p></Collapsible>
With Callbacks
Any Paragon component or export may be added to the code example.
<Collapsibletitle="Toggle Collapsible"defaultOpenonToggle={(isOpen) => console.log('Collapsible toggled and open is: ', isOpen)}onOpen={() => console.log('Collapsible opened.')}onClose={() => console.log('Collapsible closed.')}><p>See the console.</p></Collapsible>
Advanced Usage
For needs that deviate from the three styles above, use <Collapsible.Advanced />
Bare minimum
Any Paragon component or export may be added to the code example.
<Collapsible.Advanced><Collapsible.Trigger>Toggle Collapsible</Collapsible.Trigger><Collapsible.Body><p>Your stuff goes here</p></Collapsible.Body></Collapsible.Advanced>
Card style with advanced usage
Any Paragon component or export may be added to the code example.
<Collapsible.Advanced className="collapsible-card"><Collapsible.Trigger className="collapsible-trigger d-flex"><span className="flex-grow-1">This is the title</span><Collapsible.Visible whenClosed> + </Collapsible.Visible><Collapsible.Visible whenOpen> - </Collapsible.Visible></Collapsible.Trigger><Collapsible.Body className="collapsible-body">The content</Collapsible.Body></Collapsible.Advanced>
With a close button
Any Paragon component or export may be added to the code example.
<Collapsible.Advanced className="collapsible-card" defaultOpen><Collapsible.Trigger className="collapsible-trigger d-flex"><span className="flex-grow-1">This is the title</span><Collapsible.Visible whenClosed> + </Collapsible.Visible><Collapsible.Visible whenOpen> - </Collapsible.Visible></Collapsible.Trigger><Collapsible.Body className="collapsible-body"><p>The content</p><Collapsible.Trigger closeOnly tag="a" className="btn btn-outline-primary">Close</Collapsible.Trigger></Collapsible.Body></Collapsible.Advanced>
onOpen, onClose and onToggle callbacks
See the developer console for logging.
Any Paragon component or export may be added to the code example.
<Collapsible.AdvancedclassName="collapsible-card-lg"onToggle={(isOpen) => console.log('Collapsible toggled and open is: ', isOpen)}onOpen={() => console.log('Collapsible opened.')}onClose={() => console.log('Collapsible closed.')}><Collapsible.Trigger className="collapsible-trigger"><h4 className="flex-grow-1">I'm a heading</h4><Collapsible.Visible whenClosed>+</Collapsible.Visible><Collapsible.Visible whenOpen>-</Collapsible.Visible></Collapsible.Trigger><Collapsible.Body className="collapsible-body"><p>Your stuff goes here.</p><Collapsible.Trigger closeOnly tag="a" className="btn btn-outline-primary">Close</Collapsible.Trigger></Collapsible.Body></Collapsible.Advanced>
Controlled usage
Any Paragon component or export may be added to the code example.
function() {const [collapseIsOpen, setCollapseOpen] = React.useState(true);return (<Collapsible.Advancedopen={collapseIsOpen}onToggle={isOpen => setCollapseOpen(!isOpen)}className="collapsible-card"><Collapsible.Trigger className="collapsible-trigger"><h4 className="flex-grow-1">I'm a heading</h4><Collapsible.Visible whenClosed>+</Collapsible.Visible><Collapsible.Visible whenOpen>-</Collapsible.Visible></Collapsible.Trigger><Collapsible.Body className="collapsible-body"><p>Your stuff goes here.</p><Collapsible.Trigger closeOnly tag="a" className="btn btn-outline-primary">Close</Collapsible.Trigger></Collapsible.Body></Collapsible.Advanced>);}
Imperative methods
If you need to open or close the Collapsible intermittently due to an event,
such as loading the page or clicking a link, you can open or close
an uncontrolled Collapsible by getting a ref to the component and calling
collapsibleRef.open()
or collapsibleRef.close()
. The internal state of
the component will be updated accordingly.
Theme Variables (SCSS)#
$collapsible-card-spacer-y: .5rem !default;$collapsible-card-spacer-x: .5rem !default;$collapsible-card-spacer-y-lg: $card-spacer-y !default;$collapsible-card-spacer-x-lg: $card-spacer-x !default;$collapsible-card-body-spacer-left: .75rem !default;$collapsible-card-spacer-icon: 2.5rem !default;$collapsible-basic-spacer-y: .5rem !default;$collapsible-basic-spacer-x: .5rem !default;$collapsible-basic-spacer-icon: .625rem !default;
Props API#
- children
node
RequiredSpecifies contents of the component.
- className
string
Specifies class name to append to the base element.
- defaultOpen
bool
DefaultfalseSpecifies whether the
Collapsible
should be initially open. - iconWhenClosed
element
Default<Icon src={ExpandMore} />Specifies icon to show when
Collapsible
is closed. - iconWhenOpen
element
Default<Icon src={ExpandLess} />Specifies icon to show when
Collapsible
is open. - onClose
func
Callback fired when
Collapsible
closes. - onOpen
func
Callback fired when
Collapsible
opens. - onToggle
func
Callback fired when
Collapsible's
state is toggled. - open
bool
Specifies whether
Collapsible
is open. - styling
enum
'basic' | 'card' | 'card-lg'Default'card'Specifies style variant.
- title
node
RequiredSpecifies title.
- unmountOnExit
bool
DefaulttrueUnmount the component (remove it from the DOM) when it is collapsed
- children
node
Specifies contents of the component.
- className
string
Specifies classname to append to the base element.
- defaultOpen
bool
DefaultfalseSpecifies whether
Collapsible
should be initially open. - open
bool
Specifies whether
Collapsible
is open. - onToggle
func
Callback fired when
Collapsible's
state is toggled. - onOpen
func
Callback fired when
Collapsible
opens. - onClose
func
Callback fired when
Collapsible
closes. - unmountOnExit
bool
DefaulttrueUnmount the component (remove it from the DOM) when it is collapsed.
- children
node
Specifies contents of the component.
- tag
string
Default'div'Specifies content's base element.
- transitionWrapper
element
Specifies transition element.
- children
node
Specifies contents of the component.
- tag
string
|elementType
Default'div'Specifies base element.
- openOnly
bool
DefaultfalseSpecifies whether toggling
Collapsible's
state will always trigger only open action. - closeOnly
bool
DefaultfalseSpecifies whether toggling
Collapsible's
state will always trigger only close action. - onClick
func
Callback fired when element gets clicked.
- onKeyDown
func
Callback fired when a key is pressed.
- children
node
Specifies contents of the component.
- whenOpen
bool
DefaultfalseSpecifies whether the content should be visible when
Collapsible
is open. - whenClosed
bool
DefaultfalseSpecifies whether the content should be visible when
Collapsible
is closed.
Usage Insights#
Collapsible
Project Name | Paragon Version | Instance Count | |
---|---|---|---|
frontend-app-account | 20.36.0 | 4 | |
frontend-app-admin-portal | 20.26.3 | 3 | |
frontend-app-communications | 20.30.1 | 2 | |
frontend-app-course-authoring | 20.32.0 | 1 | |
frontend-app-gradebook | 19.25.4 | 4 | |
frontend-app-learner-portal-enterprise | 20.36.0 | 4 | |
frontend-app-learner-portal-programs | 20.28.4 | 1 | |
frontend-app-learning | 20.28.4 | 1 | |
frontend-app-ora-grading | 20.30.0 | 1 | |
frontend-app-payment | 20.30.1 | 1 | |
frontend-app-program-console | 20.32.0 | 2 | |
frontend-app-publisher | 20.28.5 | 1 | |
frontend-learner-portal-base | 12.2.0 | 1 | |
prospectus | 20.32.3 | 12 |
CollapsibleAdvanced
Project Name | Paragon Version | Instance Count | |
---|---|---|---|
frontend-app-course-authoring | 20.32.0 | 1 | |
frontend-app-discussions | 20.15.0 | 3 | |
frontend-app-learner-portal-enterprise | 20.36.0 | 2 | |
frontend-app-learning | 20.28.4 | 3 | |
frontend-app-ora-grading | 20.30.0 | 1 | |
frontend-app-publisher | 20.28.5 | 1 | |
frontend-lib-content-components | 20.34.0 | 8 | |
prospectus | 20.32.3 | 3 |
CollapsibleBody
Project Name | Paragon Version | Instance Count | |
---|---|---|---|
frontend-app-course-authoring | 20.32.0 | 1 | |
frontend-app-discussions | 20.15.0 | 3 | |
frontend-app-learner-portal-enterprise | 20.36.0 | 2 | |
frontend-app-learning | 20.28.4 | 3 | |
frontend-app-ora-grading | 20.30.0 | 1 | |
frontend-lib-content-components | 20.34.0 | 7 | |
prospectus | 20.32.3 | 3 |
CollapsibleTrigger
Project Name | Paragon Version | Instance Count | |
---|---|---|---|
frontend-app-course-authoring | 20.32.0 | 1 | |
frontend-app-discussions | 20.15.0 | 3 | |
frontend-app-learner-portal-enterprise | 20.36.0 | 2 | |
frontend-app-learning | 20.28.4 | 3 | |
frontend-app-ora-grading | 20.30.0 | 1 | |
frontend-app-publisher | 20.28.5 | 1 | |
frontend-lib-content-components | 20.34.0 | 3 | |
prospectus | 20.32.3 | 1 |
CollapsibleVisible
Project Name | Paragon Version | Instance Count | |
---|---|---|---|
frontend-app-course-authoring | 20.32.0 | 4 | |
frontend-app-discussions | 20.15.0 | 6 | |
frontend-app-learner-portal-enterprise | 20.36.0 | 4 | |
frontend-app-learning | 20.28.4 | 6 | |
frontend-app-ora-grading | 20.30.0 | 2 | |
frontend-app-publisher | 20.28.5 | 2 | |
frontend-lib-content-components | 20.34.0 | 4 | |
prospectus | 20.32.3 | 6 |