Package Exports
- @workday/canvas-kit-labs-react-side-panel
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@workday/canvas-kit-labs-react-side-panel) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Canvas Kit React Side Panel
A collapsable side panel
Installation
yarn add @workday/canvas-kit-labs-react-side-panel
Usage
import SidePanel, {useSidePanel} from '@workday/canvas-kit-labs-react-side-panel';
/**
* A SidePanel is made up of three components
* - The panel itself <SidePanel>
* - An accessible name (either an existing element or you can use <span hidden>)
* - A control that expands/collapses the SidePanel, usually <SidePanel.ToggleButton> as a child of <SidePanel>
* A convenience hook is available for setting the state of the SidePanel along with the proper aria- attributes
*
*/
const {panelProps, labelProps, controlProps} = useSidePanel();
<SidePanel {...panelProps}>
<SidePanel.ToggleButton {...controlProps} />
<h3 {...labelProps}>Tasks Panel</h3>
{/* Your panel contents */}
</SidePanel>
Hooks
useSidePanel
This hook manages the state and aria-
attributes for the SidePanel. It takes in a config
object:
intialExpanded: boolean
: Specifies the initial expanded/collapsed state of the side panel.panelId: string
: specifies the id of the panel, ifundefined
then this hook will generate onelabelId: string
specifies the id of the label, ifundefined
then this hook will generate one
and returns:
expanded: boolean
: The state for theSidePanel
'sexpanded
prop.setExpanded: (expanded?: boolean) => void
: A function that sets theexpanded
state.panelProps: object
: Contains theexpanded
,id
, andaria-labelledby
props for theSidePanel
(or equivalent) component.labelProps: object
: Contains theid
prop for the label.controlProps: object
: Contains thearia-controls
,aria-expanded
,aria-labelledby
, and click handler for the side panel's control element.
Usage
const [expanded, setExpanded, panelProps, labelProps, controlProps] = useSidePanel({
initialExpanded: true,
panelId: 'side-panel-1',
labelId: 'side-panel-label-1'
});
<SidePanel {...panelProps}>
{/* Make sure the control is first focusable */}
<SidePanel.ToggleButton {...controlProps} />
<span hidden {...labelProps}>Example Panel</span>
{/* Rest of the children here */}
</SidePanel>
or
<SidePanel {...panelProps}>
{/* If you want to use your own button */}
<CustomControl {...controlProps} />
{/* If you already have an element that can be an accessible name for the panel */}
<h3 {...labelProps}>Example Panel</h3>
</SidePanel>
Static Properties
ToggleButton
<SidePanel.ToggleButton>
is a control that is meant to toggle betweenexpanded = true
andexpanded = false
states. It must be used within theSidePanel
component as a child. Use in conjunction withuseSidePanel
'scontrolProps
, otherwise it does not come with explicitonClick
handlers.
This is a standard Canvas Kit
IconButton
and will accept those props.
Important: For accessibility purposes, it must be the first focusable element. We recommend that you keep it as the first child of
SidePanel
.
Component Props
Required
None
Optional
as: React.ElementType
The element the side panel will render as.
Default: 'section'
collapsedWidth: number | string
The width of the component (in
px
if it's anumber
) when it is collapsed.
Default: 64
expanded: boolean
If true, sets the expanded state of the side panel
Default: false
expandedWidth: number | string
The width of the component (in
px
if it's anumber
) when it is expanded.
Default: 320
origin: 'left' | 'right'
Which side the side panel is meant to originate from.
Default: 'left'
onExpandedChange: (expanded?: boolean) => void
The function called when the side panel's
expanded
state changes. States like'collapsing'
and'expanding'
are tracked in another callback:onStateChange
onStateTransition: (state?: SidePanelTransitionStates) => void
The function called when the side panel is transitioning between states. Use this to track when the side panel is animating between 'collapsed', 'collapsing', 'expanded', and 'expanding' states. This can be particularly helpful if child components need to react specifically to these states.
variant: SidePanelVariant
The style variant of the side panel. 'standard' is with a
soap100
background, no depth. 'alternate' is afrenchVanilla100
background with a level 3 depth.
Default: 'standard'