Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ART-7: Collapse and expand sidebar in the desktop version #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ const config = {
{ src: "/js/gtag.js", async: true },
],

plugins: [
require.resolve('./plugins/sidebarButton'),
],

themeConfig:
/** @type {import('@docusaurus/preset-classic').ThemeConfig} */
({
Expand Down
5,791 changes: 3,869 additions & 1,922 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"write-heading-ids": "docusaurus write-heading-ids"
},
"dependencies": {
"@docusaurus/core": "3.5.2",
"@docusaurus/preset-classic": "3.5.2",
"@docusaurus/core": "^3.6.1",
"@docusaurus/preset-classic": "^3.6.1",
"@docusaurus/theme-search-algolia": "3.5.2",
"@mdx-js/react": "^3.0.0",
"clsx": "^1.2.1",
Expand Down
47 changes: 47 additions & 0 deletions plugins/sidebarButton/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import SidebarButton from '../../src/components/SidebarButton';

function renderButtonComponent() {
const sidebarNavElement = document.querySelector('nav.sidebar_re4s');

if (sidebarNavElement) {
let buttonContainer = sidebarNavElement.querySelector('#sidebar-button-container');

if (!buttonContainer) {
buttonContainer = document.createElement('div');
buttonContainer.id = 'sidebar-button-container';

sidebarNavElement.appendChild(buttonContainer);
}

if (!buttonContainer.hasChildNodes()) {
const root = createRoot(buttonContainer);
root.render(<SidebarButton />);
}

buttonContainer.style.position = 'sticky';
buttonContainer.style.bottom = '0';
buttonContainer.style.width = '100%';
buttonContainer.style.left = '0';
buttonContainer.style.textAlign = 'center';
buttonContainer.style.maxHeight = `calc(100vh - (var(--ifm-navbar-height) + 2rem))`;
}
}

function handleResize() {
const sidebarNavElement = document.querySelector('nav.sidebar_re4s');
const buttonContainer = sidebarNavElement?.querySelector('#sidebar-button-container');

if (buttonContainer) {
buttonContainer.remove();
}

renderButtonComponent();
}

window.addEventListener('resize', handleResize);

export function onRouteDidUpdate() {
setTimeout(renderButtonComponent, 0);
}
10 changes: 10 additions & 0 deletions plugins/sidebarButton/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');

module.exports = function () {
return {
name: 'sidebar-button',
getClientModules() {
return [path.resolve(__dirname, './client')];
},
};
};
1 change: 1 addition & 0 deletions src/components/assets/chevrons-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions src/components/sidebarButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { useState } from 'react';

const SidebarButton = () => {
const [isOpen, setIsOpen] = useState(true);

const toggleNav = () => {
setIsOpen(!isOpen);

const sidebarNav = document.querySelector('nav.sidebar_re4s');
const aside = document.querySelector('aside');
const svgB = document.querySelector('#svg-button');
const sidebarNavWidth = parseInt(getComputedStyle(aside).width, 10) - 32;
const sidebarNavHeight = getComputedStyle(aside).height;
const navButton = document.querySelector('#sidebar-button-container button');
const navButtonContainer = document.querySelector('#sidebar-button-container');
if (aside && sidebarNav && navButtonContainer) {
sidebarNav.style.transition = 'width 0.3s ease';
sidebarNav.style.width = isOpen ? '70px' : `${sidebarNavWidth}px`;
aside.style.visibility = isOpen ? 'hidden' : 'visible';
navButton.style.visibility = 'visible';
svgB.style.transform = isOpen ? 'rotate(180deg)' : 'rotate(0deg)';
navButtonContainer.style.height = isOpen ? sidebarNavHeight : '50px';
}
};

return (
<button
style={{
padding: '10px 20px',
backgroundColor: '#1b1b1d',
border: '1px solid rgba(255, 210, 0, 1)',
cursor: 'pointer',
fontSize: '16px',
width: '100%',
height: '100%',
textAlign: 'center',
}}
onClick={toggleNav}
>
<svg id="svg-button" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" style={{fill: 'rgba(255, 210, 0, 1)'}}><path d="m8.121 12 4.94-4.939-2.122-2.122L3.879 12l7.06 7.061 2.122-2.122z"></path><path d="M17.939 4.939 10.879 12l7.06 7.061 2.122-2.122L15.121 12l4.94-4.939z"></path></svg>

</button>
);
};

export default SidebarButton;
Loading