Drawer
<gstock-drawer>
|
GstockDrawer
Placement
Use the placement attribute to make the drawer slide in from the end.
This can be start, end, top or
bottom By default, drawers slide in from the start.
<gstock-drawer label="Drawer" placement="start">
This drawer slides in from the start.
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
<gstock-drawer label="Drawer" placement="end">
This drawer slides in from the end.
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
<gstock-drawer label="Drawer" placement="top">
This drawer slides in from the top.
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
<gstock-drawer label="Drawer" placement="bottom">
This drawer slides in from the bottom.
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
<gstock-button class="start">From Start</gstock-button>
<gstock-button class="end">From End</gstock-button>
<gstock-button class="top">From Top</gstock-button>
<gstock-button class="bottom">From Bottom</gstock-button>
<script type="module">
const drawer = document.querySelector('gstock-drawer');
const drawerStart = document.querySelector('gstock-drawer[placement="start"]');
const drawerEnd = document.querySelector('gstock-drawer[placement="end"]');
const drawerTop = document.querySelector('gstock-drawer[placement="top"]');
const drawerBottom = document.querySelector('gstock-drawer[placement="bottom"]');
const startOpenButton = document.querySelector('gstock-button[class="start"]');
const startCloseButton = drawerStart.querySelector('gstock-button[variant="outlined" ]');
const endOpenButton = document.querySelector('gstock-button[class="end"]');
const endCloseButton = drawerEnd.querySelector('gstock-button[variant="outlined" ]');
const topOpenButton = document.querySelector('gstock-button[class="top"]');
const topCloseButton = drawerTop.querySelector('gstock-button[variant="outlined" ]');
const bottomOpenButton = document.querySelector('gstock-button[class="bottom"]');
const bottomCloseButton = drawerBottom.querySelector('gstock-button[variant="outlined" ]');
startOpenButton.addEventListener('click', () => drawerStart.show());
startCloseButton.addEventListener('click', () => drawerStart.hide());
endOpenButton.addEventListener('click', () => drawerEnd.show());
endCloseButton.addEventListener('click', () => drawerEnd.hide());
topOpenButton.addEventListener('click', () => drawerTop.show());
topCloseButton.addEventListener('click', () => drawerTop.hide());
bottomOpenButton.addEventListener('click', () => drawerBottom.show());
bottomCloseButton.addEventListener('click', () => drawerBottom.hide());
</script>
Contained to an Element
By default, drawers slide out of their
containing block, which is usually the viewport. To make a drawer slide out of a parent element,
add the contained attribute to the drawer and apply
position: relative to its parent.
Unlike normal drawers, contained drawers are not modal. This means they do not show an overlay, they do not trap focus, and they are not dismissible with Escape. This is intentional to allow users to interact with elements outside of the drawer.
<div class="preview__canvas--box">
The drawer will be contained to this box. This content won't shift or be affected in any way when the drawer opens.
<gstock-drawer label="Drawer" contained style="--size: 50%;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
</div>
<gstock-button>Toggle Drawer</gstock-button>
<script type="module">
const drawer = document.querySelector('gstock-drawer');
const openButton = drawer.parentElement.nextElementSibling;
const closeButton = drawer.querySelector('gstock-button[variant="outlined" ]');
openButton.addEventListener('click', () => (drawer.open = !drawer.open));
closeButton.addEventListener('click', () => drawer.hide());
</script>
Custom Size
Use the --size custom property to set the drawer’s size. This will be
applied to the drawer’s width or height depending on its placement.
<gstock-drawer label="Drawer" style="--size: 50vw;">
This drawer is always 50% of the viewport.
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
<gstock-button>Open Drawer</gstock-button>
<script type="module">
const drawer = document.querySelector('gstock-drawer');
const openButton = drawer.nextElementSibling;
const closeButton = drawer.querySelector('gstock-button[variant="outlined" ]');
openButton.addEventListener('click', () => drawer.show());
closeButton.addEventListener('click', () => drawer.hide());
</script>
Scrolling
By design, a drawer’s height will never exceed 100% of its container. As such, drawers will not scroll with the page to ensure the header and footer are always accessible to the user.
Scroll down and give it a try! 👇
<gstock-drawer label="Drawer">
<div style="height: 150vh; border: dashed 2px var(--gstock-legacy-color-grayscale-200); padding: 0 1rem;">
<p>Scroll down and give it a try! 👇</p>
</div>
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
<gstock-button>Open Drawer</gstock-button>
<script type="module">
const drawer = document.querySelector('gstock-drawer');
const openButton = drawer.nextElementSibling;
const closeButton = drawer.querySelector('gstock-button[variant="outlined" ]');
openButton.addEventListener('click', () => drawer.show());
closeButton.addEventListener('click', () => drawer.hide());
</script>
Header Actions
The header shows a functional close button by default. You can use the
header-actions slot to add additional
icon buttons if needed.
<gstock-drawer label="Drawer">
<gstock-icon-button class="new-window" slot="header-actions" icon="link" variant="plain"></gstock-icon-button>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<gstock-button slot="footer" variant="outlined">Close</gstock-button>
</gstock-drawer>
<gstock-button>Open Drawer</gstock-button>
<script type="module">
const drawer = document.querySelector('gstock-drawer');
const openButton = drawer.nextElementSibling;
const closeButton = drawer.querySelector('gstock-button[variant="outlined"]');
const newWindowButton = drawer.querySelector('.new-window');
openButton.addEventListener('click', () => drawer.show());
closeButton.addEventListener('click', () => drawer.hide());
newWindowButton.addEventListener('click', () => window.open(location.href));
</script>
Prevent closure
By default, drawers will close when the user clicks the close button, clicks the overlay, or presses the Escape key. In most cases, the default behavior is the best behavior in terms of UX. However, there are situations where this may be undesirable, such as when data loss will occur.
To keep the drawer open in such cases, you can cancel the
gstock-request-close-event event. When canceled, the drawer will remain
open and pulse briefly to draw the user’s attention to it.
You can use event.detail.source to determine what triggered the request
to close. This example prevents the drawer from closing when the overlay is clicked,
but allows the close button or Escape to dismiss it.
<gstock-drawer label="Drawer">
This drawer will not close when you click on the overlay.
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
<gstock-button>Open Drawer</gstock-button>
<script type="module">
const drawer = document.querySelector('gstock-drawer');
const openButton = drawer.nextElementSibling;
const closeButton = drawer.querySelector('gstock-button[variant="outlined" ]');
openButton.addEventListener('click', () => drawer.show());
closeButton.addEventListener('click', () => drawer.hide());
// Prevent the drawer from closing when the user clicks on the overlay
drawer.addEventListener('gstock-request-close-event', event => {
if (event.detail.source === 'overlay') {
event.preventDefault();
}
});
</script>
Customizing Initial Focus
By default, the drawer’s panel will gain focus when opened. This allows a subsequent
tab press to focus on the first tabbable element in the drawer. If you want a
different element to have focus, add the autofocus attribute to it as
shown below.
<gstock-drawer label="Drawer">
<gstock-input autofocus placeholder="I will have focus when the drawer is opened"></gstock-input>
<gstock-button slot="footer" variant="outlined" >Close</gstock-button>
</gstock-drawer>
<gstock-button>Open Drawer</gstock-button>
<script type="module">
const drawer = document.querySelector('gstock-drawer');
const input = drawer.querySelector('gstock-input');
const openButton = drawer.nextElementSibling;
const closeButton = drawer.querySelector('gstock-button[variant="outlined" ]');
openButton.addEventListener('click', () => drawer.show());
closeButton.addEventListener('click', () => drawer.hide());
</script>
You can further customize initial focus behavior by canceling the
gstock-initial-focus-event event and setting focus yourself inside
the event handler.