Sidebar

Sidebar, also known as Drawer, is a container component displayed as an overlay.


import { Sidebar } from 'primereact/sidebar';
         

Sidebar is used as a container and visibility is controlled with a binding to visible and onHide event callback.


<div className="card flex justify-content-center">
    <Sidebar visible={visible} onHide={() => setVisible(false)}>
        <h2>Sidebar</h2>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        </p>
    </Sidebar>
    <Button icon="pi pi-arrow-right" onClick={() => setVisible(true)} />
</div>
         

Sidebar location is configured with the position property that can take left, right, top and bottom as a value.


<div className="flex gap-2 justify-content-center">
    <Button icon="pi pi-arrow-right" onClick={() => setVisibleLeft(true)} />
    <Button icon="pi pi-arrow-left" onClick={() => setVisibleRight(true)} />
    <Button icon="pi pi-arrow-down" onClick={() => setVisibleTop(true)} />
    <Button icon="pi pi-arrow-up" onClick={() => setVisibleBottom(true)} />
</div>

<Sidebar visible={visibleLeft} position="left" onHide={() => setVisibleLeft(false)}>
    <h2>Left Sidebar</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </p>
</Sidebar>

<Sidebar visible={visibleRight} position="right" onHide={() => setVisibleRight(false)}>
    <h2>Right Sidebar</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </p>
</Sidebar>

<Sidebar visible={visibleTop} position="top" onHide={() => setVisibleTop(false)}>
    <h2>Top Sidebar</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </p>
</Sidebar>

<Sidebar visible={visibleBottom} position="bottom" onHide={() => setVisibleBottom(false)}>
    <h2>Bottom Sidebar</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </p>
</Sidebar>
         

Sidebar dimension can be defined with style or className properties which can also be responsive when used with a CSS utility library like PrimeFlex.


<div className="card flex justify-content-center">
    <Sidebar visible={visible} onHide={() => setVisible(false)} fullScreen>
        <h2>Sidebar</h2>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        </p>
    </Sidebar>
    <Button icon="pi pi-th-large" onClick={() => setVisible(true)} />
</div>
         

Sidebar can cover the whole page when fullScreen property is enabled.


<div className="card flex justify-content-center">
    <Sidebar visible={visible} onHide={() => setVisible(false)}>
        <h2>Sidebar</h2>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
        </p>
    </Sidebar>
    <Button icon="pi pi-arrow-right" onClick={() => setVisible(true)} />
</div>
         

Additional content at the header section is provided using the icons property.


<Sidebar visible={visible} onHide={() => setVisible(false)} icons={customIcons}>
    <h2>Sidebar</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </p>
</Sidebar>
<Button icon="pi pi-plus" onClick={() => setVisible(true)} />
         

Following is the list of structural style classes.

NameElement
p-sidebarContainer element
p-sidebar-leftContainer element of left sidebar.
p-sidebar-rightContainer element of right sidebar.
p-sidebar-topContainer element of top sidebar.
p-sidebar-bottomContainer element of bottom sidebar.
p-sidebar-fullContainer element of a full screen sidebar.
p-sidebar-activeContainer element when sidebar is visible.
p-sidebar-closeClose anchor element.
p-sidebar-smSmall sized sidebar.
p-sidebar-mdMedium sized sidebar.
p-sidebar-lgLarge sized sidebar.
p-sidebar-viewThe page view is displayed according to the sidebar position.
p-sidebar-contentContent of sidebar.
p-sidebar-maskModal layer of the sidebar.
Accessibility guide documents the specification of this component based on WCAG guidelines, the implementation is in progress.

Screen Reader

Sidebar component uses complementary role by default, since any attribute is passed to the root element aria role can be changed depending on your use case and additional attributes like aria-labelledby can be added. In addition aria-modal is added since focus is kept within the sidebar when opened.

It is recommended to use a trigger component that can be accessed with keyboard such as a button, if not adding tabIndex would be necessary.

Trigger element also requires aria-expanded and aria-controls to be handled explicitly.


<Button icon="pi pi-arrow-right" onClick={(e) => setVisible(true)} aria-controls={visible ? 'sbar' : null} aria-expanded={visible ? true : false}/>

<Sidebar id="sidebar" visible={visible} onHide={() => setVisible(false)} role="region">
Content
</Sidebar>
 

Overlay Keyboard Support

KeyFunction
tabMoves focus to the next the focusable element within the sidebar.
shift + tabMoves focus to the previous the focusable element within the sidebar.
escapeCloses the dialog if closeOnEscape is true.

Close Button Keyboard Support

KeyFunction
enterCloses the sidebar.
spaceCloses the sidebar.