Skip to content

Commit

Permalink
Add scroll to shop sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
BerglundDaniel committed Dec 18, 2023
1 parent ad3d99d commit ca8799c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions public/scss/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ body {
position: fixed;
top: 0px;
bottom: 0px;
overflow-y: auto;
}
}

Expand Down
29 changes: 25 additions & 4 deletions public/ts/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useRef, useState } from "preact/hooks";
import { ComponentChildren } from "preact";
import { logout } from "./common";
import Cart from "./cart";
Expand All @@ -15,11 +16,28 @@ const NavItem = ({ url, icon, children }: { url: string, icon: string, children:
}

export const Sidebar = ({ cart, className = "" }: { cart: { cart: Cart, productData: ProductData } | null, className?: string }) => {
const sidebarRef = useRef<HTMLDivElement>(null);
const [isSidebarScrollable, setIsSidebarScrollable] = useState(false);

useEffect((): (() => void) => {
const checkSidebarHeight = () => {
if (sidebarRef.current) {
setIsSidebarScrollable(sidebarRef.current.scrollHeight > sidebarRef.current.clientHeight);
}
}
checkSidebarHeight();
window.addEventListener("resize", checkSidebarHeight);

return () => {
window.removeEventListener("resize", checkSidebarHeight);
};
}, []);

let path = location.pathname.trim();
if (path.endsWith("/")) {
path = path.substring(0, path.length - 1);
}
return <div id="left-sidebar">
return <div id="left-sidebar" ref={sidebarRef} className={isSidebarScrollable ? "scrollable" : ""}>
<div className={`sidebar-fixed-content ${className}`}>
<img className="makerspace-logo" src={`${window.staticBasePath}/images/logo-transparent-500px-300x210.png`} />
<ul className="uk-nav uk-nav-default">
Expand All @@ -43,9 +61,12 @@ export const Sidebar = ({ cart, className = "" }: { cart: { cart: Cart, productD
}}><span uk-icon="sign-out"></span> Logga ut</a>
</li>

{cart !== null && <li className={`uk-button uk-button-primary sidebar-buy-btn ${cart.cart.items.length === 0 ? 'cart-empty' : ''}`}>
<a href="/shop/cart"><span uk-icon="cart"></span> Betala<span id="cart-sum">{Cart.formatCurrency(cart.cart.sum(cart.productData.id2item))}</span></a>
</li>
{cart !== null &&
<li className={`uk-button uk-button-primary sidebar-buy-btn ${cart.cart.items.length === 0 ? 'cart-empty' : ''}`}>
<a href="/shop/cart"><span uk-icon="cart"></span> Betala
<span id="cart-sum">{Cart.formatCurrency(cart.cart.sum(cart.productData.id2item))}</span>
</a>
</li>
}
</ul>
</div>
Expand Down

0 comments on commit ca8799c

Please sign in to comment.