|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {FOCUS_RING_CLASSES} from '@material/web/labs/gb/components/focus/focus-ring.js'; |
| 8 | +import {PSEUDO_CLASSES} from '@material/web/labs/gb/components/shared/pseudo-classes.js'; |
| 9 | +import {Directive, directive} from 'lit/directive.js'; |
| 10 | +import {classMap, type ClassInfo} from 'lit/directives/class-map.js'; |
| 11 | + |
| 12 | +/** Card color configuration types. */ |
| 13 | +export type CardColor = 'elevated' | 'filled' | 'outlined'; |
| 14 | + |
| 15 | +/** Card color configurations. */ |
| 16 | +export const CARD_COLORS = { |
| 17 | + elevated: 'elevated', |
| 18 | + filled: 'filled', |
| 19 | + outlined: 'outlined', |
| 20 | +} as const; |
| 21 | + |
| 22 | +/** Card classes. */ |
| 23 | +export const CARD_CLASSES = { |
| 24 | + card: 'card', |
| 25 | + cardElevated: 'card-elevated', |
| 26 | + cardFilled: 'card-filled', |
| 27 | + cardOutlined: 'card-outlined', |
| 28 | + hover: PSEUDO_CLASSES.hover, |
| 29 | + focus: PSEUDO_CLASSES.focus, |
| 30 | + disabled: PSEUDO_CLASSES.disabled, |
| 31 | +} as const; |
| 32 | + |
| 33 | +/** The state provided to the `cardClasses()` function. */ |
| 34 | +export interface CardClassesState { |
| 35 | + /** The color of the card. */ |
| 36 | + color?: CardColor; |
| 37 | + /** Whether the card is interactive. */ |
| 38 | + interactive?: boolean; |
| 39 | + /** Emulates `:hover`. */ |
| 40 | + hover?: boolean; |
| 41 | + /** Emulates `:focus`. */ |
| 42 | + focus?: boolean; |
| 43 | + /** Emulates `:disabled`. */ |
| 44 | + disabled?: boolean; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Returns the card classes to apply to an element based on the given state. |
| 49 | + * |
| 50 | + * @param state The state of the card. |
| 51 | + * @return An object of class names and truthy values if they apply. |
| 52 | + */ |
| 53 | +export function cardClasses({ |
| 54 | + color, |
| 55 | + interactive = false, |
| 56 | + hover = false, |
| 57 | + focus = false, |
| 58 | + disabled = false, |
| 59 | +}: CardClassesState = {}): ClassInfo { |
| 60 | + return { |
| 61 | + [FOCUS_RING_CLASSES.focusRingOuter]: interactive, |
| 62 | + [CARD_CLASSES.card]: true, |
| 63 | + [CARD_CLASSES.cardElevated]: color === CARD_COLORS.elevated, |
| 64 | + [CARD_CLASSES.cardFilled]: color === CARD_COLORS.filled, |
| 65 | + [CARD_CLASSES.cardOutlined]: color === CARD_COLORS.outlined || !color, |
| 66 | + [CARD_CLASSES.hover]: hover, |
| 67 | + [CARD_CLASSES.focus]: focus, |
| 68 | + [CARD_CLASSES.disabled]: disabled, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +/** The state provided to the `card()` directive. */ |
| 73 | +export interface CardDirectiveState extends CardClassesState { |
| 74 | + /** Additional classes to apply to the element. */ |
| 75 | + classes?: ClassInfo; |
| 76 | +} |
| 77 | + |
| 78 | +class CardDirective extends Directive { |
| 79 | + render(state: CardDirectiveState = {}) { |
| 80 | + return classMap({ |
| 81 | + ...(state.classes || {}), |
| 82 | + ...cardClasses(state), |
| 83 | + }); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * A Lit directive that adds card styling and functionality to its element. |
| 89 | + * |
| 90 | + * @example |
| 91 | + * ```ts |
| 92 | + * html` |
| 93 | + * <div class="${card({color: 'filled'})} flex flex-row p-4"> |
| 94 | + * Card content |
| 95 | + * </div> |
| 96 | + * ` |
| 97 | + * ``` |
| 98 | + */ |
| 99 | +export const card = directive(CardDirective); |
0 commit comments