|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {Directive, directive} from 'lit/directive.js'; |
| 8 | +import {classMap, type ClassInfo} from 'lit/directives/class-map.js'; |
| 9 | + |
| 10 | +/** Divider classes. */ |
| 11 | +export const DIVIDER_CLASSES = { |
| 12 | + divider: 'divider', |
| 13 | + dividerVertical: 'divider-vertical', |
| 14 | +} as const; |
| 15 | + |
| 16 | +/** The state provided to the `dividerClasses()` function. */ |
| 17 | +export interface DividerClassesState { |
| 18 | + /** Whether the divider is vertical. */ |
| 19 | + vertical?: boolean; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Returns the divider classes to apply to an element based on the given state. |
| 24 | + * |
| 25 | + * @param state The state of the divider. |
| 26 | + * @return An object of class names and truthy values if they apply. |
| 27 | + */ |
| 28 | +export function dividerClasses({ |
| 29 | + vertical = false, |
| 30 | +}: DividerClassesState = {}): ClassInfo { |
| 31 | + return { |
| 32 | + [DIVIDER_CLASSES.divider]: true, |
| 33 | + [DIVIDER_CLASSES.dividerVertical]: vertical, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +/** The state provided to the `divider()` directive. */ |
| 38 | +export interface DividerDirectiveState extends DividerClassesState { |
| 39 | + /** Additional classes to apply to the element. */ |
| 40 | + classes?: ClassInfo; |
| 41 | +} |
| 42 | + |
| 43 | +class DividerDirective extends Directive { |
| 44 | + render(state: DividerDirectiveState = {}) { |
| 45 | + return classMap({ |
| 46 | + ...(state.classes || {}), |
| 47 | + ...dividerClasses(state), |
| 48 | + }); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * A Lit directive that adds divider styling to its element. |
| 54 | + * |
| 55 | + * @example |
| 56 | + * ```ts |
| 57 | + * html` |
| 58 | + * <div class="flex flex-col"> |
| 59 | + * <div>Vertical</div> |
| 60 | + * <hr class="${divider()}"> |
| 61 | + * <div>Items</div> |
| 62 | + * </div> |
| 63 | + * |
| 64 | + * <div class="flex flex-row"> |
| 65 | + * <div>Horizontal</div> |
| 66 | + * <hr class="${divider({vertical: true})}"> |
| 67 | + * <div>Items</div> |
| 68 | + * </div> |
| 69 | + * `; |
| 70 | + * ``` |
| 71 | + */ |
| 72 | +export const divider = directive(DividerDirective); |
0 commit comments