Skip to content

Commit

Permalink
Remove responsive references and add javascript snippet for how to ac…
Browse files Browse the repository at this point in the history
…hieve it (#265)

This PR updates the documentation around sidebars to remove unneeded references and also includes a javascript snippet for how to make your sidebar responsive.
  • Loading branch information
Jeremy-Walton authored Jan 31, 2025
1 parent 7d630b9 commit fae03f1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
2 changes: 0 additions & 2 deletions src/stories/Components/Sidebar/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const createSidebarItem = ({ type, icon, label }, activeLink) => {

export const createSidebar = ({
size = 'drawer',
responsive = false,
style = 'default',
brand = true,
padded = false,
Expand Down Expand Up @@ -60,7 +59,6 @@ export const createSidebar = ({
'sidebar',
style === 'default' ? '' : `sidebar--${style}`,
`sidebar--${size}`,
responsive ? 'sidebar--responsive' : '',
padded ? 'sidebar--padded' : '',
]
.filter(Boolean)
Expand Down
46 changes: 43 additions & 3 deletions src/stories/Components/Sidebar/Sidebar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ For instructions on how to integrate a sidebar into your applications layout, se

`.sidebar--drawer`, `.sidebar--compact`, and `.sidebar--rail` are used to denote which style of sidebar you would like to use.

`.sidebar--responsive` can be added to a drawer to allow for a sidebar that responsively changes from the larger state (compact or drawer) to the rail when the screen is smaller. e.g.

```html
<div class="sidebar sidebar--drawer sidebar--responsive">...</div>
<div class="sidebar sidebar--drawer">...</div>
```

Any [Button](?path=/docs/navigation-components-button--docs#default) style can be used for the links.
Expand All @@ -33,6 +31,48 @@ Any [Button](?path=/docs/navigation-components-button--docs#default) style can b
- Compact will use the `.icon-with-label` style
- Rail will use the `.btn--icon` style and hide the label

## Responsive Sidebar

The sidebar can be made responsive with a little snippet of Javascript. This allows for the sidebar to change from the larger state (compact or drawer) to the rail when the screen is smaller. e.g.

```js
const sidebarStyleOptions = {
drawer: 'sidebar--drawer',
compact: 'sidebar--compact',
rail: 'sidebar--rail',
}

const getSidebarStyle = (width) => {
let newStyle = sidebarStyleOptions['drawer']

if (window.innerWidth <= 1024) {
newStyle = sidebarStyleOptions['compact']
}

if (window.innerWidth <= 768) {
newStyle = sidebarStyleOptions['rail']
}

return newStyle
}

const applySidebarStyle = (newStyle) => {
const sidebar = document.getElementById('sidebar')
sidebar.classList.remove(sidebarStyleOptions['drawer'])
sidebar.classList.remove(sidebarStyleOptions['compact'])
sidebar.classList.remove(sidebarStyleOptions['rail'])
sidebar.classList.add(newStyle)
}

// Initial Page Load
applySidebarStyle(getSidebarStyle(window.innerWidth))

// Window Resize
window.addEventListener('resize', (event) => {
applySidebarStyle(getSidebarStyle(window.innerWidth))
})
```

## Playground

<Canvas of={SidebarStories.DefaultDrawer} />
Expand Down
1 change: 0 additions & 1 deletion src/stories/Components/Sidebar/Sidebar.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default {
control: { type: 'select' },
options: ['drawer', 'compact', 'rail'],
},
responsive: { control: 'boolean' },
padded: { control: 'boolean' },
style: {
control: { type: 'select' },
Expand Down

0 comments on commit fae03f1

Please sign in to comment.