New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@vrembem/drawer

Package Overview
Dependencies
Maintainers
1
Versions
117
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vrembem/drawer

A container component that slides in from the left or right. Typically containing menus, search or other content.

  • 1.7.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
162
increased by24.62%
Maintainers
1
Weekly downloads
 
Created
Source

Drawer

A container component that slides in from the left or right. Typically containing menus, search or other content.

npm version

Documentation

Installation

npm install @vrembem/drawer

Styles

@use "@vrembem/drawer";

JavaScript

import { Drawer } from '@vrembem/drawer';
const drawer = new Drawer({ autoInit: true });

Markup

Drawers are composed using classes for styling and data attributes for JavaScript functionality. To link a drawer toggle to a drawer, use a unique identifier as the values for both of their respective data attributes. Close buttons are left value-less and should be placed inside the drawer element they’re meant to close.

  • data-drawer="[unique-id]"
  • data-drawer-toggle="[unique-id]"
  • data-drawer-close
<div class="drawer__wrapper">
  <aside data-drawer="[unique-id]" class="drawer">
    <div class="drawer__item">
      <button data-drawer-close>...</button>
    </div>
  </aside>
  <div class="drawer__main">
    <button data-drawer-toggle="[unique-id]">...</button>
  </div>
</div>

The dialog component is a great fit for composing a drawer’s content.

<aside data-drawer="[unique-id]" class="drawer">
  <div class="drawer__item dialog">
    <div class="dialog__header">
      ...
      <button data-drawer-close>...</button>
    </div>
    <div class="dialog__body">
      ...
    </div>
    <div class="dialog__footer">
      ...
    </div>
  </div>
</aside>
data-drawer-breakpoint

In cases where you'd like a drawer to switch to a drawer modal on a specific breakpoint, use the data-drawer-breakpoint data attribute with either a breakpoint key or a specific pixel value.

<!-- Switches to modal below `md` breakpoint viewports -->
<aside data-drawer="[unique-id]" data-drawer-breakpoint="md" class="drawer">
  ...
</aside>

<!-- Switches to modal below 900px viewports -->
<aside data-drawer="[unique-id]" data-drawer-breakpoint="900px" class="drawer">
  ...
</aside>
data-drawer-focus

If a drawer has the attribute tabindex="-1", it will be given focus when it's opened. If focus on a specific element inside a drawer is prefered, give it the data-drawer-focus attribute. The focus in either case is returned to the trigger element once the drawer is closed. Focus handling can be disabled using the { focus: false } setting.

<div cass="drawer__wrapper">
  <!-- Focus the drawer on open -->
  <aside data-drawer="[unique-id]" class="drawer" tabindex="-1">
    ...
  </aside>
  <!-- Focus the close button on open -->
  <aside data-drawer="[unique-id]" class="drawer">
    ...
    <button data-drawer-close data-drawer-focus>Close</button>
  </aside>
  <div class="drawer__main">
    <!-- Return focus to toggle on close -->
    <button data-drawer-toggle="[unique-id]">Drawer Toggle</button>
  </div>
</div>
Drawer State

By default, the state of a drawer is saved to local storage and applied persistently under the "DrawerState" local storage variable. Set saveState: false to disable save state. Use saveKey: "[CUSTOM-KEY]" to change the key that save state is stored under.

Modifiers

drawer_modal

Convert a drawer into it’s modal state with the drawer_modal modifier class. Only one modal can be open at a time.

<div class="drawer__wrapper">
  <aside data-drawer="[unique-id]" class="drawer drawer_modal">
    ...
  </aside>
  <div class="drawer__main">
    <button data-drawer-toggle="[unique-id]">
      ...
    </button>
  </div>
</div>

drawer_pos_[value]

Drawers can slide in from the left or right using the position modifiers:

  • drawer_pos_left
  • drawer_pos_right
<div class="drawer__wrapper">
  <aside data-drawer="[unique-id]" class="drawer drawer_pos_left">
    ...
  </aside>
  <aside data-drawer="[unique-id]" class="drawer drawer_pos_right">
    ...
  </aside>
  <div class="drawer__main">
    <button data-drawer-toggle="[unique-id]">
      ...
    </button>
    <button data-drawer-toggle="[unique-id]">
      ...
    </button>
  </div>
</div>

If a position modifier is not provided, the drawer will appear based on it’s location in the DOM relative to the main content area and other drawers.

Customization

Sass Variables

VariableDefaultDescription
$prefix-blocknullString to prefix blocks with.
$prefix-element"__"String to prefix element with.
$prefix-modifier"_"String to prefix modifier with.
$prefix-modifier-value"_"String to prefix modifier values with.
$width18emThe width of drawers.
$travel5emDistance that drawers travel during their transition.
$transition-duration0.3sDuration of drawer transition.
$transition-timing-functioncubic-bezier(0.4, 0, 0.2, 1)Timing function used for drawer transitions.
$item-background#f5f5f5Background color applied to drawer items.
$item-bordernullBorder applied to drawer items with position modifiers. Shown on side of drawers facing drawer main.
$item-box-shadownoneBox shadow applied to drawer items.
$item-sep-border-colornullBorder color applied to dialog components within drawer items.
$modal-zindex900Modal z-index to help control the stack order. Should be highest priority as modal.
$modal-item-background#fffBackground color applied to modal drawer items.
$modal-item-box-shadowSee: core.$box-shadow-24dpBox shadow applied to modal drawer items.
$modal-background#424242Background color of modal screen.
$modal-background-alpha0.8The alpha channel for the modal screen.

JavaScript Events

  • drawer:opened Emits when the drawer has opened.
  • drawer:closed Emits when the drawer has closed.
  • drawer:breakpoint Emits when the drawer has hit a breakpoint.
  • drawer:toModal Emits when the drawer is switched to it's modal state.
  • drawer:toDefault Emits when the drawer is switched to it's default state.

JavaScript Options

KeyDefaultDescription
autoInitfalseAutomatically instantiates the instance.
dataDrawer'drawer'Data attribute for a drawer.
dataToggle'drawer-toggle'Data attribute for a drawer toggle trigger.
dataClose'drawer-close'Data attribute for a drawer close trigger.
dataBreakpoint'drawer-breakpoint'Data attribute for setting a drawer's breakpoint.
dataFocus'drawer-focus'Data attribute for setting a drawer's focus element.
stateOpen'is-opened'Class used for open state.
stateOpening'is-opening'Class used for transitioning to open state.
stateClosing'is-closing'Class used for transitioning to closed state.
stateClosed'is-closed'Class used for closed state.
classModal'drawer_modal'Class used for toggling the drawer modal state.
customEventPrefix'drawer:'Prefix to be used on custom events.
breakpointscore.breakpointsAn object with key/value pairs defining a breakpoints set.
focustrueToggles the focus handling feature.
saveStatetrueToggles the save state feature.
saveKey"DrawerState"Defines the localStorage key where drawer states are saved.

JavaScript API

MethodDescription
drawer.init()Initializes the drawer instance.
drawer.destroy()Destroys and cleans up the drawer instantiation.
drawer.toggle(drawerKey, callback)Toggles a drawer provided the drawer key and optional callback.
drawer.open(drawerKey, callback)Opens a drawer provided the drawer key and optional callback.
drawer.close(drawerKey, callback)Closes a drawer provided the drawer key and optional callback.
drawer.breakpoint.init()Initializes the drawer breakpoint feature.
drawer.breakpoint.destroy()Destroys the drawer breakpoint feature.
drawer.breakpoint.check()Force a check if any drawers meet their breakpoint condition.
drawer.switchToModal(drawer)Switches a drawer to it's modal state.
drawer.switchToDefault(drawer)Switches a drawer to it's default state.

Keywords

FAQs

Package last updated on 21 Jun 2020

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