|
| 1 | +/** |
| 2 | + * Shared component recipes. |
| 3 | + * |
| 4 | + * These framework-neutral builders are the devframe "components": each returns |
| 5 | + * the canonical `df-*` class string for an element, so React (`className=`), |
| 6 | + * Svelte (`class=`) and vanilla DOM (`el.className =`) all describe the same |
| 7 | + * button, panel, tab or nav the same way and render identically. |
| 8 | + * |
| 9 | + * Because the class strings are assembled at runtime, the `df-*` vocabulary is |
| 10 | + * safelisted by the preset (see {@link DF_SAFELIST}) so UnoCSS always emits it |
| 11 | + * regardless of static extraction. |
| 12 | + */ |
| 13 | + |
| 14 | +/** Join truthy class fragments into a single class string. */ |
| 15 | +export function cx(...parts: Array<string | false | null | undefined>): string { |
| 16 | + return parts.filter(Boolean).join(' ') |
| 17 | +} |
| 18 | + |
| 19 | +export type ButtonVariant = 'primary' | 'secondary' | 'outline' | 'ghost' | 'destructive' | 'link' |
| 20 | +export type ButtonSize = 'md' | 'sm' | 'lg' | 'icon' | 'icon-sm' |
| 21 | + |
| 22 | +export interface ButtonProps { |
| 23 | + variant?: ButtonVariant |
| 24 | + size?: ButtonSize |
| 25 | + /** Extra classes appended after the recipe. */ |
| 26 | + class?: string |
| 27 | +} |
| 28 | + |
| 29 | +/** A button. `df-btn` + a variant, optionally a non-default size. */ |
| 30 | +export function button({ variant = 'primary', size = 'md', class: extra }: ButtonProps = {}): string { |
| 31 | + return cx('df-btn', `df-btn-${variant}`, size !== 'md' && `df-btn-${size}`, extra) |
| 32 | +} |
| 33 | + |
| 34 | +export type BadgeVariant = 'primary' | 'secondary' | 'success' | 'warning' | 'destructive' | 'outline' |
| 35 | + |
| 36 | +export interface BadgeProps { |
| 37 | + variant?: BadgeVariant |
| 38 | + class?: string |
| 39 | +} |
| 40 | + |
| 41 | +/** A solid/semantic badge (the variant class already includes the `df-badge` base). */ |
| 42 | +export function badge({ variant = 'secondary', class: extra }: BadgeProps = {}): string { |
| 43 | + return cx(`df-badge-${variant}`, extra) |
| 44 | +} |
| 45 | + |
| 46 | +/** A soft, palette-driven tag (`df-tag-blue`, `df-tag-amber`, …). */ |
| 47 | +export function tag(color: string, extra?: string): string { |
| 48 | + return cx(`df-tag-${color}`, extra) |
| 49 | +} |
| 50 | + |
| 51 | +/** The container for a set of segmented tabs. */ |
| 52 | +export function tabsList(extra?: string): string { |
| 53 | + return cx('df-tabs-list', extra) |
| 54 | +} |
| 55 | + |
| 56 | +/** A segmented tab (active state is driven by `data-state="active"` on the element). */ |
| 57 | +export function tab(extra?: string): string { |
| 58 | + return cx('df-tab', extra) |
| 59 | +} |
| 60 | + |
| 61 | +export interface NavTabProps { |
| 62 | + active?: boolean |
| 63 | + class?: string |
| 64 | +} |
| 65 | + |
| 66 | +/** A closeable navigation tab (terminal sessions, open documents, …). */ |
| 67 | +export function navTab({ active = false, class: extra }: NavTabProps = {}): string { |
| 68 | + return cx('df-navtab', active && 'df-navtab-active', extra) |
| 69 | +} |
| 70 | + |
| 71 | +/** A top navigation bar. */ |
| 72 | +export function nav(extra?: string): string { |
| 73 | + return cx('df-nav', extra) |
| 74 | +} |
| 75 | + |
| 76 | +/** A secondary toolbar bar. */ |
| 77 | +export function toolbar(extra?: string): string { |
| 78 | + return cx('df-toolbar', extra) |
| 79 | +} |
| 80 | + |
| 81 | +/** A card surface. */ |
| 82 | +export function card(extra?: string): string { |
| 83 | + return cx('df-card', extra) |
| 84 | +} |
| 85 | + |
| 86 | +/** A flat panel surface. */ |
| 87 | +export function panel(extra?: string): string { |
| 88 | + return cx('df-panel', extra) |
| 89 | +} |
| 90 | + |
| 91 | +/** A text input / textarea. */ |
| 92 | +export function input(extra?: string): string { |
| 93 | + return cx('df-input', extra) |
| 94 | +} |
| 95 | + |
| 96 | +/** An inline link. */ |
| 97 | +export function link(extra?: string): string { |
| 98 | + return cx('df-link', extra) |
| 99 | +} |
| 100 | + |
| 101 | +export type DotState = 'running' | 'idle' | 'error' |
| 102 | + |
| 103 | +/** A status dot for a lifecycle state. */ |
| 104 | +export function dot(state: DotState, extra?: string): string { |
| 105 | + return cx('df-dot', `df-dot-${state}`, extra) |
| 106 | +} |
| 107 | + |
| 108 | +/** An indeterminate spinner. */ |
| 109 | +export function spinner(extra?: string): string { |
| 110 | + return cx('df-spinner', extra) |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * The full fixed `df-*` vocabulary. The preset safelists this so the runtime |
| 115 | + * builders above always have CSS to resolve to, even though their class strings |
| 116 | + * never appear literally in scanned source. A representative set of palette |
| 117 | + * tags is included for {@link tag}. |
| 118 | + */ |
| 119 | +export const DF_SAFELIST: string[] = [ |
| 120 | + // buttons |
| 121 | + 'df-btn', |
| 122 | + 'df-btn-primary', |
| 123 | + 'df-btn-secondary', |
| 124 | + 'df-btn-outline', |
| 125 | + 'df-btn-ghost', |
| 126 | + 'df-btn-destructive', |
| 127 | + 'df-btn-link', |
| 128 | + 'df-btn-sm', |
| 129 | + 'df-btn-lg', |
| 130 | + 'df-btn-icon', |
| 131 | + 'df-btn-icon-sm', |
| 132 | + // badges |
| 133 | + 'df-badge', |
| 134 | + 'df-badge-primary', |
| 135 | + 'df-badge-secondary', |
| 136 | + 'df-badge-success', |
| 137 | + 'df-badge-warning', |
| 138 | + 'df-badge-destructive', |
| 139 | + 'df-badge-outline', |
| 140 | + // tabs + bars |
| 141 | + 'df-tabs-list', |
| 142 | + 'df-tab', |
| 143 | + 'df-navtab', |
| 144 | + 'df-navtab-active', |
| 145 | + 'df-nav', |
| 146 | + 'df-toolbar', |
| 147 | + // surfaces + controls |
| 148 | + 'df-card', |
| 149 | + 'df-panel', |
| 150 | + 'df-input', |
| 151 | + 'df-link', |
| 152 | + // status |
| 153 | + 'df-dot', |
| 154 | + 'df-dot-running', |
| 155 | + 'df-dot-idle', |
| 156 | + 'df-dot-error', |
| 157 | + 'df-spinner', |
| 158 | + // common palette tags |
| 159 | + 'df-tag-blue', |
| 160 | + 'df-tag-amber', |
| 161 | + 'df-tag-green', |
| 162 | + 'df-tag-red', |
| 163 | + 'df-tag-sky', |
| 164 | + 'df-tag-violet', |
| 165 | + 'df-tag-rose', |
| 166 | +] |
0 commit comments