From aa3c4db5e2ef3c96aa32c3935a4f40dcf146e1bb Mon Sep 17 00:00:00 2001 From: Claas Date: Tue, 5 Sep 2023 14:56:27 +0200 Subject: [PATCH] Add support for small badge --- .../navigation-drawer/src/DimNavigationBar.ts | 8 ++++- .../src/DimNavigationItem.ts | 29 +++++++++++++++++-- .../src/DimNavigationRail.ts | 13 +++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/components/navigation-drawer/src/DimNavigationBar.ts b/components/navigation-drawer/src/DimNavigationBar.ts index abe35a6..0a3ed64 100644 --- a/components/navigation-drawer/src/DimNavigationBar.ts +++ b/components/navigation-drawer/src/DimNavigationBar.ts @@ -70,12 +70,18 @@ export class DimNavigationBar extends NavigationHost { border-radius: var(--md-sys-shape-corner-large); - & svg { + & svg:first-of-type { height: 24px; width: 24px; } /* Badge */ + & svg:nth-of-type(2) { + position: absolute; + inset: 4px 16px auto auto; + color: var(--md-sys-color-error); + } + & span { position: absolute; inset: 2px auto auto 50%; diff --git a/components/navigation-drawer/src/DimNavigationItem.ts b/components/navigation-drawer/src/DimNavigationItem.ts index 6298829..5986e16 100644 --- a/components/navigation-drawer/src/DimNavigationItem.ts +++ b/components/navigation-drawer/src/DimNavigationItem.ts @@ -1,4 +1,4 @@ -import { LitElement, html, nothing } from 'lit'; +import { LitElement, html, nothing, svg } from 'lit'; import { property } from 'lit/decorators.js'; const iconOutlinedName = 'icon-outlined'; @@ -89,6 +89,31 @@ export class DimNavigationItem extends LitElement { } } +function badge(text?: string) { + switch (text) { + // Empty badge + case '': + return svg` + + + + `; + + case undefined: + return nothing; + + default: { + // Maximum badge label length is 4 + const asNumber = Number(text); + if (Number.isNaN(asNumber)) return html`${text.slice(0, 4)}`; + + // For numbers we allow 3 digits and add plus at the end + if (asNumber > 999) return html`999+`; + + return html`${text}`; + } + } +} /** * Renders a navigation item for the navigation rail and navigatio bar but not the navigation drawer as the layout is * different for that component. @@ -102,7 +127,7 @@ export function renderNavigationItem(item: DimNavigationItem) {
${item.isActive ? item.iconFilled : item.iconOutlined} - ${item.badge ? html`${item.badge}` : nothing} + ${badge(item.badge)}
${item.label ? html`${item.label}` : nothing} diff --git a/components/navigation-drawer/src/DimNavigationRail.ts b/components/navigation-drawer/src/DimNavigationRail.ts index b951756..3e44d10 100644 --- a/components/navigation-drawer/src/DimNavigationRail.ts +++ b/components/navigation-drawer/src/DimNavigationRail.ts @@ -69,12 +69,20 @@ export class DimNavigationRail extends NavigationHost { border-radius: 100px; - & svg { + & svg:first-of-type { height: 24px; width: 24px; } /* Badge */ + --_no-label-badge-position-top: 16px; + & svg:nth-of-type(2) { + position: absolute; + + inset: var(--_no-label-badge-position-top) 16px auto auto; + color: var(--md-sys-color-error); + } + --_badge-position-top: 14px; & span { position: absolute; @@ -113,9 +121,10 @@ export class DimNavigationRail extends NavigationHost { } /* Has label */ - &:has(span) div { + &:has(div + span) div { --_icon-container-padding-y: 4px; --_badge-position-top: 2px; + --_no-label-badge-position-top: 4px; } /* States */ -- knotmirror 0.0.1