diff --git a/components/navigation-drawer/src/DimNavigationBar.ts b/components/navigation-drawer/src/DimNavigationBar.ts new file mode 100644 index 0000000..d7fc4c2 --- /dev/null +++ b/components/navigation-drawer/src/DimNavigationBar.ts @@ -0,0 +1,204 @@ +import { css, html, nothing } from 'lit'; +import NavigationHost from './NavigationHost.js'; + +export class DimNavigationBar extends NavigationHost { + static styles = css` + :host { + position: fixed; + inset: auto 0 0 0; + + --_background-color: var(--md-sys-color-surface); + background-color: var(--_background-color); + color: var(--md-sys-color-on-surface-variant); + + font-family: var(--md-sys-typescale-label-medium-font-family-name); + line-height: var(--md-sys-typescale-label-medium-line-height); + font-size: var(--md-sys-typescale-label-medium-font-size); + font-style: var(--md-sys-typescale-label-medium-font-family-style); + letter-spacing: var(--md-sys-typescale-label-medium-letter-spacing); + font-weight: var(--md-sys-typescale-label-medium-font-weight); + } + + nav { + box-sizing: border-box; + + height: 80px; + padding: 0 8px; + + & ol { + all: unset; + display: flex; + gap: 8px; + justify-content: space-between; + + list-style-type: none; + + & li { + width: 100%; + + & a { + all: unset; + display: flex; + flex-direction: column; + align-items: center; + gap: 4px; + + width: 100%; + padding: 12px 0 16px 0; + + cursor: pointer; + + /* Icon container */ + & div { + height: 24px; + width: 24px; + padding: 4px 20px; + + position: relative; + + border-radius: var(--md-sys-shape-corner-large); + & svg { + height: 24px; + width: 24px; + } + + /* Badge */ + & span { + position: absolute; + inset: 2px auto auto 50%; + + display: flex; + align-items: center; + justify-content: center; + + height: 14px; + width: auto; + min-width: 16px; + min-height: 16px; + padding: 0px 4px; + box-sizing: border-box; + + border-radius: 8px; + background-color: var(--md-sys-color-error); + color: var(--md-sys-color-on-error); + + text-align: center; + + font-family: var( + --md-sys-typescale-label-small-font-family-name + ); + line-height: var(--md-sys-typescale-label-small-line-height); + font-size: var(--md-sys-typescale-label-small-font-size); + font-style: var( + --md-sys-typescale-label-small-font-family-style + ); + letter-spacing: var( + --md-sys-typescale-label-small-letter-spacing + ); + font-weight: var(--md-sys-typescale-label-small-font-weight); + } + } + + /* States */ + /* Inactive */ + &:hover div { + background-color: color-mix( + in srgb, + var(--md-sys-color-on-surface-variant) + var(--md-sys-state-hover-state-layer-opacity), + var(--_background-color) + ); + } + + &:focus div { + background-color: color-mix( + in srgb, + var(--md-sys-color-on-surface-variant) + var(--md-sys-state-focus-state-layer-opacity), + var(--_background-color) + ); + } + + &:active div { + background-color: color-mix( + in srgb, + var(--md-sys-color-on-surface-variant) + var(--md-sys-state-pressed-state-layer-opacity), + var(--_background-color) + ); + } + + &[active] { + color: var(--md-sys-color-on-surface); + + & div { + background-color: var(--md-sys-color-secondary-container); + + /* Icon color is different to text */ + & > svg { + color: var(--md-sys-color-on-secondary-container); + } + } + + &:hover div { + background-color: color-mix( + in srgb, + var(--md-sys-color-on-surface) + var(--md-sys-state-hover-state-layer-opacity), + var(--md-sys-color-secondary-container) + ); + + color: var(--md-sys-on-secondary-container); + } + + &:focus div { + background-color: color-mix( + in srgb, + var(--md-sys-color-on-secondary-container) + var(--md-sys-state-focus-state-layer-opacity), + var(--md-sys-color-secondary-container) + ); + + color: var(--md-sys-on-secondary-container); + } + + &:active div { + background-color: color-mix( + in srgb, + var(--md-sys-color-on-secondary-container) + var(--md-sys-state-focus-state-layer-opacity), + var(--md-sys-color-secondary-container) + ); + + color: var(--md-sys-on-secondary-container); + } + } + } + } + } + } + `; + + render() { + return html` + + `; + } +} diff --git a/components/navigation-drawer/src/DimNavigationHostContext.ts b/components/navigation-drawer/src/DimNavigationHostContext.ts index 599ebfa..f566149 100644 --- a/components/navigation-drawer/src/DimNavigationHostContext.ts +++ b/components/navigation-drawer/src/DimNavigationHostContext.ts @@ -1,9 +1,7 @@ import { createContext } from '@lit-labs/context'; -import type { DimNavigation } from './DimNavigation.js'; - -export type { DimNavigation } from './DimNavigation.js'; +import NavigationHost from './NavigationHost.js'; export const dimNavigationHostContextKey = 'dim-navigation-host'; -export const dimNavigationHostContext = createContext( +export const dimNavigationHostContext = createContext( dimNavigationHostContextKey ); diff --git a/components/navigation-drawer/src/DimNavigationItem.ts b/components/navigation-drawer/src/DimNavigationItem.ts index 06a0659..befeeb3 100644 --- a/components/navigation-drawer/src/DimNavigationItem.ts +++ b/components/navigation-drawer/src/DimNavigationItem.ts @@ -1,9 +1,12 @@ -import { LitElement, css, html, nothing } from 'lit'; +import { LitElement, css, html } from 'lit'; import { property } from 'lit/decorators.js'; import { consume } from '@lit-labs/context'; import { dimNavigationHostContext } from './DimNavigationHostContext.js'; import { DimNavigation } from './DimNavigation.js'; +const iconOutlinedName = 'icon-outlined'; +const iconFilledName = 'icon-filled'; + export class DimNavigationItem extends LitElement { static styles = css` :host { @@ -237,7 +240,6 @@ export class DimNavigationItem extends LitElement { width: 24px; } - /* Hide badge for now */ & span:nth-of-type(2) { display: none; @@ -345,7 +347,8 @@ export class DimNavigationItem extends LitElement { @consume({ context: dimNavigationHostContext }) private navigationHost?: DimNavigation; - connectedCallback(): void { + // connectedCallback(): void { + firstUpdated(): void { super.connectedCallback(); /** @@ -372,16 +375,48 @@ export class DimNavigationItem extends LitElement { this.navigationHost.unregister(this); } + /** + * + * @internal + */ + iconOutlined?: Element[]; + + /** + * + * @internal + */ + iconFilled?: Element[]; + + #iconOutlinedChanged(event: Event) { + if ( + !(event.target instanceof HTMLSlotElement) || + event.target.name !== iconOutlinedName + ) + return; + + this.iconOutlined = event.target.assignedElements(); + } + + #iconFilledChanged(event: Event) { + if ( + !(event.target instanceof HTMLSlotElement) || + event.target.name !== iconFilledName + ) + return; + + this.iconFilled = event.target.assignedElements(); + } + protected render() { return html` -
  • - - - - ${this.label} - ${this.badge ? html`${this.badge}` : nothing} - -
  • + + `; } } diff --git a/components/navigation-drawer/src/NavigationHost.ts b/components/navigation-drawer/src/NavigationHost.ts new file mode 100644 index 0000000..7fc95a6 --- /dev/null +++ b/components/navigation-drawer/src/NavigationHost.ts @@ -0,0 +1,81 @@ +import { LitElement } from 'lit'; +import { provide } from '@lit-labs/context'; +import { DimNavigationItem } from './DimNavigationItem.js'; +import { dimNavigationHostContext } from './DimNavigationHostContext.js'; + +/** + * @internal + */ +export default abstract class NavigationHost extends LitElement { + /** + * The drawer is the host of the navigation items and manages the active item as there can only be one navigation item + * active at a time but the navigation items are not aware of each other which is why it is delegated to the host. + */ + protected items: DimNavigationItem[] = []; + + @provide({ context: dimNavigationHostContext }) + private navigationHost: typeof this = this; + + static #findActive(items: DimNavigationItem[]): DimNavigationItem | null { + for (const item of items) { + if (item.href !== window.location.href) continue; + + // Only the first item is marked as active as multiple items being active is unwanted behavior + return item; + } + + return null; + } + + /** + * Adds a navigation item to the navigation host and recalculates the current active navigation item + * @param item the navigation item to register + * @internal + */ + register(item: DimNavigationItem) { + if (this.items.includes(item)) return; + + // Items should register in the order of their DOM appearance + this.items.push(item); + + // Find active + const activeItem = NavigationHost.#findActive(this.items); + + if (activeItem) this.#setActiveItem(activeItem); + } + + /** + * Removes the navigation item from the navigation host and recalculates the active navigation element + * @param item the item to unregister + * @internal + */ + unregister(item: DimNavigationItem) { + const index = this.items.indexOf(item); + if (!index) return; + + this.items.slice(index, 1); + + const activeItem = NavigationHost.#findActive(this.items); + if (activeItem) this.#setActiveItem(activeItem); + } + + #activeItem?: DimNavigationItem; + + /** + * Sets the item as current active navigation item and unsets the old one if there was one before + * @param item the item to set as active item + */ + #setActiveItem(item: DimNavigationItem) { + // If active item did not change, avoid updating and possibly triggering render + if (this.#activeItem === item) return; + + // Unset previous active item + if (this.#activeItem) this.#activeItem.isActive = false; + + this.#activeItem = item; + this.#activeItem.isActive = true; + + // Initiate render as the active icon changes appearance + this.requestUpdate(); + } +} diff --git a/components/navigation-drawer/src/dim-navigation.ts b/components/navigation-drawer/src/dim-navigation.ts index e2d8ae2..10cc2e9 100644 --- a/components/navigation-drawer/src/dim-navigation.ts +++ b/components/navigation-drawer/src/dim-navigation.ts @@ -2,12 +2,14 @@ import { DimNavigation } from './DimNavigation.js'; import { DimDivider } from './DimDivider.js'; import { DimNavigationSection } from './DimNavigationSection.js'; import { DimNavigationItem } from './DimNavigationItem.js'; +import { DimNavigationBar } from './DimNavigationBar.js'; /** * Order of registratiaon matters! A navigation host must be registered before the navigation item. * Otherwise it seems like the navigation item is sending the "context-request" event before the navigation host has * initialized the provider listening. In this case the host is the navigation drawer */ +window.customElements.define('dim-navigation-bar', DimNavigationBar); window.customElements.define('dim-navigation', DimNavigation); window.customElements.define('dim-navigation-item', DimNavigationItem); window.customElements.define('dim-navigation-section', DimNavigationSection); diff --git a/page/index.html b/page/index.html index 70a2cbf..66cdd2d 100644 --- a/page/index.html +++ b/page/index.html @@ -180,122 +180,66 @@ Label + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + - -

    Mail

    +
    +
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Labels

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    -