Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@accessible/drawer

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@accessible/drawer

An accessible and versatile drawer component for React

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
267
decreased by-56.44%
Maintainers
1
Weekly downloads
 
Created
Source

<Drawer>

Bundlephobia Code coverage Build status NPM Version MIT License

npm i @accessible/drawer

An accessible and versatile drawer component for React. This drawer is just a few new styles on top of @accessible/modal so when using both, the drawer costs you nothing.

Features

  • Style-agnostic You can use this component with the styling library of your choice. It works with CSS-in-JS, SASS, plain CSS, plain style objects, anything!
  • Portal-friendly The drawer content will render into React portals of your choice when configured to do so.
  • a11y/aria-compliant This component works with screen readers out of the box and manages focus for you.

Quick Start

Check out the example on CodeSandbox

import {Drawer, Content, Trigger, Close} from '@accessible/drawer'

const Component = () => (
  <Drawer>
    <Content>
      <div className="my-drawer">
        <Close>
          <button>Close me</button>
        </Close>
      </div>
    </Content>

    <Trigger>
      <button>Open me</button>
    </Trigger>
  </Drawer>
)

API

Components

ComponentDescription
<Drawer>This component creates the context for your drawer box and trigger and contains some configuration options.
<Content>This component wraps any React element and turns it into a drawer box.
<Trigger>This component wraps any React element and turns it into a drawer trigger.
<Close>This is a convenience component that wraps any React element and adds an onClick handler to close the drawer.

Hooks

HookDescription
useDrawer()This hook provides the value of the drawer's DrawerContextValue object.
useControls()This hook provides access to the drawer's open, close, and toggle functions.
useIsOpen()This hook provides access to the drawer's isOpen value.

<Drawer>

This component creates the context for your drawer box and trigger and contains some configuration options.

Props
PropTypeDefaultRequired?Description
defaultOpenbooleanfalseNoThis sets the default open state of the drawer. By default the drawer is closed.
openbooleanundefinedNoYou can control the open/closed state of the drawer with this prop. When it isn't undefined, this value will take precedence over any calls to open(), close(), or toggle().
idstringundefinedNoBy default this component creates a unique id for you, as it is required for certain aria attributes. Supplying an id here overrides the auto id feature.
childrenReact.ReactNode | React.ReactNode[] | JSX.Element | ((context: DrawerContextValue) => React.ReactNode)undefinedNoYour drawer contents and any other children.

<Content>

This component wraps any React element and turns it into a drawer content.

Props
PropTypeDefaultRequired?Description
placement"top" | "right" | "bottom" | "left" "left"NoSets the edge of the window you want your drawer content to render on.
portalboolean | string falseNoWhen true this will render the drawer into a React portal with the id #portals. You can render it into any portal by providing its query selector here, e.g. #foobar, [data-portal=true], or .foobar.
closeOnEscapebooleantrueNoBy default the drawer will close when the Escape key is pressed. You can turn this off by providing false here.
closedClassstringundefinedNoThis class name will be applied to the child element when the drawer is closed.
openClassstring"modal--open"NoThis class name will be applied to the child element when the drawer is open.
closedStyleReact.CSSPropertiesundefinedNoThese styles will be applied to the child element when the drawer is closed in addition to the default styles that set the box's visibility.
openStyleReact.CSSPropertiesundefinedNoThese styles name will be applied to the child element when the drawer is open in addition to the default styles that set the box's visibility.
childrenReact.ReactElementundefinedYesThe child is cloned by this component and has aria attributes injected into its props as well as the events defined above.
Example
<Content>
  <div className="alert">Alert</div>
</Content>

// <div
//   class="alert"
//   aria-hidden="true"
//   aria-drawer="false"
//   id="modal--12"
//   role="content"
//   style="visibility: hidden; position: fixed; margin: auto; left: 0px; right: 0px; top: 50%; transform: translateY(-50%); z-index: 1;"
// >
//   Alert
// </div>

<Trigger>

This component wraps any React element and adds an onClick handler which toggles the open state of the drawer content.

Props
PropTypeDefaultRequired?Description
closedClassstringundefinedNoThis class name will be applied to the child element when the drawer is closed.
openClassstringundefinedNoThis class name will be applied to the child element when the drawer is open.
closedStyleReact.CSSPropertiesundefinedNoThese styles will be applied to the child element when the drawer is closed.
openStyleReact.CSSPropertiesundefinedNoThese styles name will be applied to the child element when the drawer is open.
childrenReact.ReactElementundefinedYesThe child is cloned by this component and has aria attributes injected into its props as well as the events defined above.
<Trigger on="click">
  <button className="my-button">Open me!</button>
</Trigger>

// <button
//   class="my-button"
//   aria-controls="modal--12"
//   aria-haspopup="content"
//   aria-expanded="false"
// >
//   Open me!
// </button>

<Close>

This is a convenience component that wraps any React element and adds an onClick handler which closes the drawer.

Props
PropTypeDefaultRequired?Description
childrenReact.ReactElementundefinedYesThe child is cloned by this component and has aria attributes injected into its props as well as the events defined above.
<Close>
  <button className="my-button">Close me</button>
</Close>

// <button
//   class="my-button"
//   aria-controls="modal--12"
//   aria-haspopup="content"
//   aria-expanded="false"
// >
//   Close me
// </button>

useDrawer()

This hook provides the value of the drawer's DrawerContextValue object

Example
const Component = () => {
  const {open, close, toggle, isOpen} = useDrawer()
  return <button onClick={toggle}>Toggle the drawer</button>
}

DrawerContextValue

interface DrawerContextValue {
  isOpen: boolean
  open: () => void
  close: () => void
  toggle: () => void
  id: string
}

useControls()

This hook provides access to the drawer's open, close, and toggle functions

Example
const Component = () => {
  const {open, close, toggle} = useControls()
  return (
    <Content>
      <div className="my-drawer">
        <button onClick={close}>Close me</button>
      </div>
    </Content>
  )
}

useIsOpen()

This hook provides access to the drawer's isOpen value

Example
const Component = () => {
  const isOpen = useIsOpen()
  return (
    <Content>
      <div className="my-drawer">Am I open? {isOpen ? 'Yes' : 'No'}</div>
    </Content>
  )
}

LICENSE

MIT

Keywords

FAQs

Package last updated on 18 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc