@thekestongroup/ui 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,6 +28,8 @@ import { SidebarItem, SidebarProps } from '../Sidebar/Sidebar.vue';
28
28
  export interface AppShellProps {
29
29
  /** Name of the app, shown in the header and as the mobile menu's title. */
30
30
  title?: string;
31
+ /** Make the header's title a link, e.g. to the app's dashboard (`to` for `linkComponent`, or an href). */
32
+ titleTo?: unknown;
31
33
  /** The signed-in user, for the account menu. Without it, Sign in is shown. */
32
34
  user?: HeaderUser;
33
35
  /** Which actions the account menu offers. Default all three. */
@@ -40,10 +42,10 @@ export interface AppShellProps {
40
42
  sections?: SidebarProps['sections'];
41
43
  /** Wide screens: show the header's menu button, which collapses the sidebar to icons. */
42
44
  collapsible?: boolean;
43
- /** Component used for sidebar items with `to`, e.g. RouterLink. */
45
+ /** Component used for sidebar items with `to`, and the title link, e.g. RouterLink. */
44
46
  linkComponent?: SidebarProps['linkComponent'];
45
47
  /** Other Header props, e.g. `{ ui: { title: '…' } }`. */
46
- header?: Omit<HeaderProps, 'title' | 'menu' | 'menuOpen' | 'sticky' | 'user' | 'userActions' | 'signIn'>;
48
+ header?: Omit<HeaderProps, 'title' | 'titleTo' | 'linkComponent' | 'menu' | 'menuOpen' | 'sticky' | 'user' | 'userActions' | 'signIn'>;
47
49
  /** Other Sidebar props, e.g. `{ label: 'Admissions' }`. */
48
50
  sidebar?: Omit<SidebarProps, 'items' | 'sections' | 'collapsible' | 'linkComponent'>;
49
51
  class?: HTMLAttributes['class'];
@@ -1,4 +1,4 @@
1
- import { HTMLAttributes } from 'vue';
1
+ import { Component, HTMLAttributes } from 'vue';
2
2
  export interface HeaderUser {
3
3
  name: string;
4
4
  email?: string;
@@ -24,6 +24,13 @@ export type HeaderUserAction = 'account-settings' | 'switch-account' | 'log-out'
24
24
  export interface HeaderProps {
25
25
  /** Name of the app or area, shown next to the menu button. */
26
26
  title?: string;
27
+ /**
28
+ * Make the title a link, e.g. to the app's dashboard: passed as `to` to
29
+ * `linkComponent` (e.g. a route), or as `href` without one.
30
+ */
31
+ titleTo?: unknown;
32
+ /** Component for the title link, e.g. RouterLink. It receives `to`. Default a plain `<a href>`. */
33
+ linkComponent?: string | Component;
27
34
  /**
28
35
  * Show the menu button, which emits `menu` when pressed. Hide it on wide
29
36
  * screens with `ui.menu`, e.g. `{ menu: 'lg:hidden' }`.
@@ -89,6 +96,7 @@ declare const __VLS_base: import('vue').DefineComponent<HeaderProps, {}, {}, {},
89
96
  }>, {
90
97
  menu: boolean;
91
98
  sticky: boolean;
99
+ linkComponent: string | Component;
92
100
  menuOpen: boolean;
93
101
  menuLabel: string;
94
102
  userActions: HeaderUserAction[];
@@ -91,10 +91,10 @@ declare const __VLS_base: import('vue').DefineComponent<__VLS_PublicProps, {}, {
91
91
  "onUpdate:collapsed"?: ((value: boolean) => any) | undefined;
92
92
  }>, {
93
93
  label: string;
94
+ linkComponent: string | Component;
94
95
  items: SidebarItem[];
95
96
  sections: SidebarSection[];
96
97
  collapsible: boolean;
97
- linkComponent: string | Component;
98
98
  }, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {}, any>;
99
99
  declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
100
100
  declare const _default: typeof __VLS_export;
@@ -1,4 +1,4 @@
1
- import { HTMLAttributes } from 'vue';
1
+ import { Component, HTMLAttributes } from 'vue';
2
2
  import { TableColumn, TableSort } from './table';
3
3
  /**
4
4
  * Rows of data with column headings: applicants, classes, invoices.
@@ -13,6 +13,11 @@ import { TableColumn, TableSort } from './table';
13
13
  * `manualSort` it only reports the sort (`v-model:sort`) so you can sort on
14
14
  * the server.
15
15
  *
16
+ * Clickable rows: `rowLink` gives each row a page to go to. The row-header
17
+ * cell (or the first column) becomes a real link, stretched over the whole
18
+ * row, so the row highlights on hover and a click anywhere on it follows the
19
+ * link. Buttons and links in other cells still work on their own.
20
+ *
16
21
  * Slots: cell-{key}, header-{key}, empty, footer (rows for a `<tfoot>`,
17
22
  * e.g. totals), caption.
18
23
  */
@@ -37,6 +42,14 @@ export interface TableProps<Row = Record<string, any>> {
37
42
  stickyHeader?: boolean;
38
43
  /** Don't sort the rows: just update `sort`, e.g. to sort on the server. */
39
44
  manualSort?: boolean;
45
+ /**
46
+ * Where each row goes when clicked: passed as `to` to `linkComponent`
47
+ * (e.g. a route), or as `href` without one. Return undefined for rows that
48
+ * don't go anywhere.
49
+ */
50
+ rowLink?: (row: Row, index: number) => unknown;
51
+ /** Component for row links, e.g. RouterLink. It receives `to`. Default a plain `<a href>`. */
52
+ linkComponent?: string | Component;
40
53
  class?: HTMLAttributes['class'];
41
54
  ui?: {
42
55
  table?: HTMLAttributes['class'];
package/dist/keston-ui.js CHANGED
@@ -479,6 +479,8 @@ var kn = ["z-50 rounded border border-black/15 bg-white text-black shadow-md out
479
479
  __name: "Header",
480
480
  props: {
481
481
  title: {},
482
+ titleTo: {},
483
+ linkComponent: { default: "a" },
482
484
  menu: {
483
485
  type: Boolean,
484
486
  default: !1
@@ -520,8 +522,11 @@ var kn = ["z-50 rounded border border-black/15 bg-white text-black shadow-md out
520
522
  "sign-in"
521
523
  ],
522
524
  setup(e, { emit: t }) {
523
- let n = e, r = t, l = i(() => On(n.user?.name ?? "")), f = i(() => n.user ? n.user.avatar || !l.value ? `Account: ${n.user.name}` : `${l.value}, account: ${n.user.name}` : "");
524
- function p(e, t) {
525
+ let n = e, r = t, l = i(() => On(n.user?.name ?? "")), f = i(() => n.user ? n.user.avatar || !l.value ? `Account: ${n.user.name}` : `${l.value}, account: ${n.user.name}` : ""), p = i(() => {
526
+ let e = n.titleTo;
527
+ if (e != null && e !== "") return n.linkComponent === "a" ? { href: e } : { to: e };
528
+ }), m = ["-mx-2 rounded px-2 py-1 transition-colors hover:bg-black/10 active:bg-black/15", "outline-2 outline-offset-1 outline-transparent focus-visible:outline-focus forced-colors:not-focus-visible:outline-0"];
529
+ function h(e, t) {
525
530
  t(), r(e === "account-settings" ? "account-settings" : e === "switch-account" ? "switch-account" : "log-out");
526
531
  }
527
532
  return (t, i) => (C(), s("header", { class: v(j(X)("flex h-12 shrink-0 items-center gap-2 border-b border-black/15 bg-white px-4 text-black", e.sticky && "sticky top-0 z-40", n.class)) }, [
@@ -544,10 +549,10 @@ var kn = ["z-50 rounded border border-black/15 bg-white text-black shadow-md out
544
549
  "class"
545
550
  ])) : o("", !0),
546
551
  D(t.$slots, "leading"),
547
- e.title || t.$slots.title ? (C(), s("span", {
548
- key: 1,
549
- class: v(j(X)("min-w-0 truncate text-base font-bold", e.ui?.title))
550
- }, [D(t.$slots, "title", {}, () => [u(A(e.title), 1)])], 2)) : o("", !0),
552
+ e.title || t.$slots.title ? (C(), a(O(p.value ? e.linkComponent : "span"), g({ key: 1 }, p.value, { class: j(X)("min-w-0 truncate text-base font-bold", p.value && m, e.ui?.title) }), {
553
+ default: z(() => [D(t.$slots, "title", {}, () => [u(A(e.title), 1)])]),
554
+ _: 3
555
+ }, 16, ["class"])) : o("", !0),
551
556
  c("div", { class: v(j(X)("flex min-w-0 flex-1 items-center", e.ui?.middle)) }, [D(t.$slots, "default")], 2),
552
557
  t.$slots.trailing || t.$slots.account || e.user || e.signIn ? (C(), s("div", {
553
558
  key: 2,
@@ -570,7 +575,7 @@ var kn = ["z-50 rounded border border-black/15 bg-white text-black shadow-md out
570
575
  key: 0,
571
576
  size: "sm",
572
577
  class: "col-start-2 justify-self-start",
573
- onClick: (e) => p("account-settings", n)
578
+ onClick: (e) => h("account-settings", n)
574
579
  }, {
575
580
  leading: z(() => [d(j(Te))]),
576
581
  default: z(() => [i[2] ||= u(" Account settings ", -1)]),
@@ -580,7 +585,7 @@ var kn = ["z-50 rounded border border-black/15 bg-white text-black shadow-md out
580
585
  D(t.$slots, "account-menu", { close: n }),
581
586
  e.userActions.includes("switch-account") || e.userActions.includes("log-out") ? (C(), s("div", er, [e.userActions.includes("switch-account") ? (C(), a(Z, {
582
587
  key: 0,
583
- onClick: (e) => p("switch-account", n)
588
+ onClick: (e) => h("switch-account", n)
584
589
  }, {
585
590
  leading: z(() => [d(j(U))]),
586
591
  default: z(() => [i[3] ||= u(" Switch account ", -1)]),
@@ -588,7 +593,7 @@ var kn = ["z-50 rounded border border-black/15 bg-white text-black shadow-md out
588
593
  }, 8, ["onClick"])) : o("", !0), e.userActions.includes("log-out") ? (C(), a(Z, {
589
594
  key: 1,
590
595
  class: "ml-auto",
591
- onClick: (e) => p("log-out", n)
596
+ onClick: (e) => h("log-out", n)
592
597
  }, {
593
598
  leading: z(() => [d(j(be))]),
594
599
  default: z(() => [i[4] ||= u(" Log out ", -1)]),
@@ -1139,6 +1144,7 @@ var ar = ["data-collapsed"], or = ["aria-label"], sr = ["data-level"], cr = {
1139
1144
  __name: "AppShell",
1140
1145
  props: /*@__PURE__*/ h({
1141
1146
  title: {},
1147
+ titleTo: {},
1142
1148
  user: {},
1143
1149
  userActions: { default: void 0 },
1144
1150
  signIn: {
@@ -1199,6 +1205,8 @@ var ar = ["data-collapsed"], or = ["aria-label"], sr = ["data-level"], cr = {
1199
1205
  }, " Skip to content ", 8, kr),
1200
1206
  d(tr, g(e.header, {
1201
1207
  title: e.title,
1208
+ "title-to": e.titleTo,
1209
+ "link-component": e.linkComponent,
1202
1210
  menu: "",
1203
1211
  sticky: "",
1204
1212
  "menu-open": s.value,
@@ -1248,6 +1256,8 @@ var ar = ["data-collapsed"], or = ["aria-label"], sr = ["data-level"], cr = {
1248
1256
  } : void 0
1249
1257
  ]), 1040, [
1250
1258
  "title",
1259
+ "title-to",
1260
+ "link-component",
1251
1261
  "menu-open",
1252
1262
  "user",
1253
1263
  "user-actions",
@@ -4045,6 +4055,8 @@ var qa = [
4045
4055
  size: { default: "md" },
4046
4056
  stickyHeader: { type: Boolean },
4047
4057
  manualSort: { type: Boolean },
4058
+ rowLink: {},
4059
+ linkComponent: { default: "a" },
4048
4060
  class: { type: [
4049
4061
  Boolean,
4050
4062
  null,
@@ -4066,25 +4078,38 @@ var qa = [
4066
4078
  function m(e) {
4067
4079
  if (e.sortable) return r.value?.key === e.key ? r.value.direction === "asc" ? "ascending" : "descending" : "none";
4068
4080
  }
4069
- let h = i(() => t.size === "sm" ? "px-4 py-2" : "px-4 py-3");
4070
- function g(e) {
4081
+ let h = i(() => (t.columns.find((e) => e.rowHeader) ?? t.columns[0])?.key);
4082
+ function _(e, n) {
4083
+ let r = t.rowLink?.(e, n);
4084
+ if (r != null && r !== "") return t.linkComponent === "a" ? { href: r } : { to: r };
4085
+ }
4086
+ let y = [
4087
+ "text-inherit no-underline outline-none group-hover/row:underline underline-offset-2",
4088
+ "after:absolute after:inset-0 after:outline-2 after:-outline-offset-2 after:outline-transparent",
4089
+ "focus-visible:after:outline-focus forced-colors:not-focus-visible:after:outline-0"
4090
+ ], b = [
4091
+ "relative cursor-pointer hover:bg-black/5",
4092
+ "[&_:is(a,button,input,select,textarea,label,[tabindex]):not([data-row-link])]:relative",
4093
+ "[&_:is(a,button,input,select,textarea,label,[tabindex]):not([data-row-link])]:z-1"
4094
+ ], w = i(() => t.size === "sm" ? "px-4 py-2" : "px-4 py-3");
4095
+ function k(e) {
4071
4096
  return X(e.align === "end" && "text-right", e.nowrap !== !1 && "whitespace-nowrap", e.class);
4072
4097
  }
4073
- let _ = T(), y = T(!1), b;
4074
- function w() {
4075
- let e = _.value;
4076
- e && (y.value = e.scrollWidth > e.clientWidth + 1 || e.scrollHeight > e.clientHeight + 1);
4098
+ let M = T(), F = T(!1), I;
4099
+ function L() {
4100
+ let e = M.value;
4101
+ e && (F.value = e.scrollWidth > e.clientWidth + 1 || e.scrollHeight > e.clientHeight + 1);
4077
4102
  }
4078
4103
  return S(() => {
4079
- w(), b = new ResizeObserver(w), _.value && (b.observe(_.value), _.value.firstElementChild && b.observe(_.value.firstElementChild));
4080
- }), x(() => b?.disconnect()), (i, b) => (C(), s("div", {
4104
+ L(), I = new ResizeObserver(L), M.value && (I.observe(M.value), M.value.firstElementChild && I.observe(M.value.firstElementChild));
4105
+ }), x(() => I?.disconnect()), (i, x) => (C(), s("div", {
4081
4106
  ref_key: "scroller",
4082
- ref: _,
4083
- class: v(j(X)("relative overflow-auto", y.value && no, t.class)),
4084
- tabindex: y.value ? 0 : void 0,
4085
- role: y.value ? "region" : void 0,
4086
- "aria-labelledby": y.value && (e.caption || i.$slots.caption) ? l : void 0,
4087
- "aria-label": y.value && !e.caption && !i.$slots.caption ? "Table" : void 0
4107
+ ref: M,
4108
+ class: v(j(X)("relative overflow-auto", F.value && no, t.class)),
4109
+ tabindex: F.value ? 0 : void 0,
4110
+ role: F.value ? "region" : void 0,
4111
+ "aria-labelledby": F.value && (e.caption || i.$slots.caption) ? l : void 0,
4112
+ "aria-label": F.value && !e.caption && !i.$slots.caption ? "Table" : void 0
4088
4113
  }, [c("table", {
4089
4114
  class: v(j(X)("w-full border-separate border-spacing-0 text-left text-sm text-black", e.ui?.table)),
4090
4115
  "aria-busy": e.loading || void 0
@@ -4098,7 +4123,7 @@ var qa = [
4098
4123
  key: e.key,
4099
4124
  scope: "col",
4100
4125
  "aria-sort": m(e),
4101
- class: v(j(X)("border-b border-black/15 font-bold text-black/65", h.value, g(e)))
4126
+ class: v(j(X)("border-b border-black/15 font-bold text-black/65", w.value, k(e)))
4102
4127
  }, [D(i.$slots, `header-${e.key}`, { column: e }, () => [e.hideLabel ? (C(), s("span", Xa, A(e.label), 1)) : e.sortable ? (C(), s("button", {
4103
4128
  key: 1,
4104
4129
  type: "button",
@@ -4120,23 +4145,36 @@ var qa = [
4120
4145
  c("tbody", { class: v(j(X)(e.loading && f.value.length && "opacity-50 transition-opacity")) }, [f.value.length ? o("", !0) : (C(), s("tr", Qa, [c("td", {
4121
4146
  colspan: e.columns.length,
4122
4147
  class: "px-4 py-8 text-center text-black/65"
4123
- }, [e.loading ? (C(), s("span", eo, [d(xn, { size: "sm" }), b[0] ||= u(" Loading… ", -1)])) : D(i.$slots, "empty", {}, () => [d(Ha, {
4148
+ }, [e.loading ? (C(), s("span", eo, [d(xn, { size: "sm" }), x[0] ||= u(" Loading… ", -1)])) : D(i.$slots, "empty", {}, () => [d(Ha, {
4124
4149
  size: "sm",
4125
4150
  description: e.emptyText,
4126
4151
  class: "p-0"
4127
4152
  }, null, 8, ["description"])], void 0, 1)], 8, $a)])), (C(!0), s(n, null, E(f.value, (t, r) => (C(), s("tr", {
4128
4153
  key: p(t, r),
4129
- class: v(j(X)("group/row", e.ui?.row))
4130
- }, [(C(!0), s(n, null, E(e.columns, (e) => (C(), a(O(e.rowHeader ? "th" : "td"), {
4131
- key: e.key,
4132
- scope: e.rowHeader ? "row" : void 0,
4133
- class: v(j(X)("border-b border-black/15 align-middle group-last/row:border-b-0", e.rowHeader ? "font-bold" : "font-normal", e.align === "end" && "tabular-nums", h.value, g(e)))
4154
+ class: v(j(X)("group/row", _(t, r) && b, e.ui?.row))
4155
+ }, [(C(!0), s(n, null, E(e.columns, (n) => (C(), a(O(n.rowHeader ? "th" : "td"), {
4156
+ key: n.key,
4157
+ scope: n.rowHeader ? "row" : void 0,
4158
+ class: v(j(X)("border-b border-black/15 align-middle group-last/row:border-b-0", n.rowHeader ? "font-bold" : "font-normal", n.align === "end" && "tabular-nums", w.value, k(n)))
4134
4159
  }, {
4135
- default: z(() => [D(i.$slots, `cell-${e.key}`, {
4160
+ default: z(() => [n.key === h.value && _(t, r) ? (C(), a(O(e.linkComponent), g({
4161
+ key: 0,
4162
+ ref_for: !0
4163
+ }, _(t, r), {
4164
+ "data-row-link": "",
4165
+ class: y
4166
+ }), {
4167
+ default: z(() => [D(i.$slots, `cell-${n.key}`, {
4168
+ row: t,
4169
+ value: t[n.key],
4170
+ index: r
4171
+ }, () => [u(A(t[n.key] ?? ""), 1)])]),
4172
+ _: 2
4173
+ }, 1040)) : D(i.$slots, `cell-${n.key}`, {
4136
4174
  row: t,
4137
- value: t[e.key],
4175
+ value: t[n.key],
4138
4176
  index: r
4139
- }, () => [u(A(t[e.key] ?? ""), 1)])]),
4177
+ }, () => [u(A(t[n.key] ?? ""), 1)], void 0, 1)]),
4140
4178
  _: 2
4141
4179
  }, 1032, ["scope", "class"]))), 128))], 2))), 128))], 2),
4142
4180
  i.$slots.footer ? (C(), s("tfoot", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thekestongroup/ui",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Keston UI — Vue 3 + Reka UI + Tailwind CSS component library",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {
@@ -60,6 +60,11 @@ type Story = StoryObj<typeof meta>;
60
60
  */
61
61
  export const Default: Story = {};
62
62
 
63
+ /** titleTo: the app's name in the header links to its dashboard (`to` for `linkComponent`, or an href). */
64
+ export const TitleLink: Story = {
65
+ args: { titleTo: "#dashboard" },
66
+ };
67
+
63
68
  /** Starts with the sidebar collapsed to icons. */
64
69
  export const Collapsed: Story = {
65
70
  args: { collapsed: true },
@@ -35,6 +35,8 @@ import Slideover from '../Slideover/Slideover.vue'
35
35
  export interface AppShellProps {
36
36
  /** Name of the app, shown in the header and as the mobile menu's title. */
37
37
  title?: string
38
+ /** Make the header's title a link, e.g. to the app's dashboard (`to` for `linkComponent`, or an href). */
39
+ titleTo?: unknown
38
40
  /** The signed-in user, for the account menu. Without it, Sign in is shown. */
39
41
  user?: HeaderUser
40
42
  /** Which actions the account menu offers. Default all three. */
@@ -47,10 +49,10 @@ export interface AppShellProps {
47
49
  sections?: SidebarProps['sections']
48
50
  /** Wide screens: show the header's menu button, which collapses the sidebar to icons. */
49
51
  collapsible?: boolean
50
- /** Component used for sidebar items with `to`, e.g. RouterLink. */
52
+ /** Component used for sidebar items with `to`, and the title link, e.g. RouterLink. */
51
53
  linkComponent?: SidebarProps['linkComponent']
52
54
  /** Other Header props, e.g. `{ ui: { title: '…' } }`. */
53
- header?: Omit<HeaderProps, 'title' | 'menu' | 'menuOpen' | 'sticky' | 'user' | 'userActions' | 'signIn'>
55
+ header?: Omit<HeaderProps, 'title' | 'titleTo' | 'linkComponent' | 'menu' | 'menuOpen' | 'sticky' | 'user' | 'userActions' | 'signIn'>
54
56
  /** Other Sidebar props, e.g. `{ label: 'Admissions' }`. */
55
57
  sidebar?: Omit<SidebarProps, 'items' | 'sections' | 'collapsible' | 'linkComponent'>
56
58
  class?: HTMLAttributes['class']
@@ -126,6 +128,8 @@ const sidebarId = `${uid}-sidebar`
126
128
  <Header
127
129
  v-bind="header"
128
130
  :title="title"
131
+ :title-to="titleTo"
132
+ :link-component="linkComponent"
129
133
  menu
130
134
  sticky
131
135
  :menu-open="menuOpen"
@@ -106,6 +106,15 @@ export const CustomUser: Story = {
106
106
  }),
107
107
  };
108
108
 
109
+ /**
110
+ * titleTo: the app's name links to its dashboard. It looks the same, with a
111
+ * ghost button's hover background and the focus ring. Pass your router's
112
+ * link as `linkComponent` (it gets `to`); without one it's a plain `<a href>`.
113
+ */
114
+ export const TitleLink: Story = {
115
+ args: { titleTo: "#dashboard" },
116
+ };
117
+
109
118
  /** Just the title: no menu button, actions or account menu. */
110
119
  export const TitleOnly: Story = {
111
120
  args: { menu: false, user: undefined, signIn: false },
@@ -1,5 +1,5 @@
1
1
  <script setup lang="ts">
2
- import { computed, type HTMLAttributes } from 'vue'
2
+ import { computed, type Component, type HTMLAttributes } from 'vue'
3
3
  import { ArrowLeftRight, LogOut, Menu, Settings } from '@lucide/vue'
4
4
  import { cn } from '../../lib/cn'
5
5
  import Avatar from '../Avatar/Avatar.vue'
@@ -34,6 +34,13 @@ export type HeaderUserAction = 'account-settings' | 'switch-account' | 'log-out'
34
34
  export interface HeaderProps {
35
35
  /** Name of the app or area, shown next to the menu button. */
36
36
  title?: string
37
+ /**
38
+ * Make the title a link, e.g. to the app's dashboard: passed as `to` to
39
+ * `linkComponent` (e.g. a route), or as `href` without one.
40
+ */
41
+ titleTo?: unknown
42
+ /** Component for the title link, e.g. RouterLink. It receives `to`. Default a plain `<a href>`. */
43
+ linkComponent?: string | Component
37
44
  /**
38
45
  * Show the menu button, which emits `menu` when pressed. Hide it on wide
39
46
  * screens with `ui.menu`, e.g. `{ menu: 'lg:hidden' }`.
@@ -79,6 +86,7 @@ const props = withDefaults(defineProps<HeaderProps>(), {
79
86
  menu: false,
80
87
  menuOpen: undefined,
81
88
  menuLabel: 'Menu',
89
+ linkComponent: 'a',
82
90
  userActions: () => ['account-settings', 'switch-account', 'log-out'],
83
91
  signIn: true,
84
92
  sticky: true
@@ -113,6 +121,19 @@ const accountLabel = computed(() => {
113
121
  return `${initials.value}, account: ${props.user.name}`
114
122
  })
115
123
 
124
+ const titleLink = computed(() => {
125
+ const target = props.titleTo
126
+ if (target === undefined || target === null || target === '') return undefined
127
+ return props.linkComponent === 'a' ? { href: target as string } : { to: target }
128
+ })
129
+
130
+ // As a link the title looks the same, with a ghost button's hover and press
131
+ // backgrounds round it; the negative margin keeps the text where it was.
132
+ const titleLinkClass = [
133
+ '-mx-2 rounded px-2 py-1 transition-colors hover:bg-black/10 active:bg-black/15',
134
+ 'outline-2 outline-offset-1 outline-transparent focus-visible:outline-focus forced-colors:not-focus-visible:outline-0'
135
+ ]
136
+
116
137
  function runAction(action: HeaderUserAction, close: () => void) {
117
138
  close()
118
139
  // Vue can't emit a name held in a variable with full typing, so switch.
@@ -148,12 +169,14 @@ function runAction(action: HeaderUserAction, close: () => void) {
148
169
 
149
170
  <slot name="leading" />
150
171
 
151
- <span
172
+ <component
173
+ :is="titleLink ? linkComponent : 'span'"
152
174
  v-if="title || $slots.title"
153
- :class="cn('min-w-0 truncate text-base font-bold', ui?.title)"
175
+ v-bind="titleLink"
176
+ :class="cn('min-w-0 truncate text-base font-bold', titleLink && titleLinkClass, ui?.title)"
154
177
  >
155
178
  <slot name="title">{{ title }}</slot>
156
- </span>
179
+ </component>
157
180
 
158
181
  <div :class="cn('flex min-w-0 flex-1 items-center', ui?.middle)">
159
182
  <slot />
@@ -1,5 +1,5 @@
1
1
  import type { Meta, StoryObj } from "@storybook/vue3-vite";
2
- import { ref } from "vue";
2
+ import { defineComponent, h, ref } from "vue";
3
3
  import { Ellipsis, Inbox } from "@lucide/vue";
4
4
  import Badge from "../Badge/Badge.vue";
5
5
  import Button from "../Button/Button.vue";
@@ -37,6 +37,17 @@ const columns: TableColumn<Applicant>[] = [
37
37
  { key: "actions", label: "Actions", hideLabel: true, align: "end" },
38
38
  ];
39
39
 
40
+ // Stands in for RouterLink: renders an <a> for `to` and reports the click
41
+ // instead of navigating, so the stories show where a row would go.
42
+ const opened = ref("");
43
+ const FakeRouterLink = defineComponent({
44
+ props: { to: { type: String, required: true } },
45
+ setup(props, { slots }) {
46
+ return () =>
47
+ h("a", { href: props.to, onClick: (e: MouseEvent) => (e.preventDefault(), (opened.value = props.to)) }, slots.default?.());
48
+ },
49
+ });
50
+
40
51
  const date = (d: Date) => d.toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric" });
41
52
 
42
53
  const meta = {
@@ -210,3 +221,50 @@ export const ManualSort: Story = {
210
221
  </div>`,
211
222
  }),
212
223
  };
224
+
225
+ /**
226
+ * rowLink: each row goes to the applicant's page. The name is a real link,
227
+ * stretched over the row, so the row highlights on hover and a click anywhere
228
+ * on it opens the page; Tab reaches the link (the ring goes round the row).
229
+ * The ⋯ button in the actions column still works on its own. Pass your
230
+ * router's link as `linkComponent` (here a stand-in that shows where it
231
+ * would go); without one the links are plain `<a href>`.
232
+ */
233
+ export const ClickableRows: Story = {
234
+ args: {
235
+ rowLink: (row) => `/applicants/${row.id}`,
236
+ linkComponent: FakeRouterLink,
237
+ },
238
+ render: (args) => ({
239
+ components: { Table, Card, Badge, Button, Ellipsis },
240
+ setup() {
241
+ const pressed = ref("");
242
+ opened.value = "";
243
+ return { args, statusColors, date, opened, pressed };
244
+ },
245
+ template: `
246
+ <div class="flex flex-col gap-3">
247
+ <Card padding="none">
248
+ <Table v-bind="args">
249
+ <template #cell-applied="{ value }">{{ date(value) }}</template>
250
+ <template #cell-score="{ value }">{{ value ?? '—' }}</template>
251
+ <template #cell-status="{ value }"><Badge size="sm" :color="statusColors[value]">{{ value }}</Badge></template>
252
+ <template #cell-actions="{ row }">
253
+ <Button variant="ghost" size="sm" square :aria-label="'More for ' + row.name" @click="pressed = 'More for ' + row.name"><Ellipsis /></Button>
254
+ </template>
255
+ </Table>
256
+ </Card>
257
+ <p class="text-sm">Opened: <code>{{ opened || 'none' }}</code> · Pressed: <code>{{ pressed || 'none' }}</code></p>
258
+ </div>`,
259
+ }),
260
+ };
261
+
262
+ /** Rows whose rowLink returns undefined (here, declined applicants) aren't clickable. */
263
+ export const SomeRowsClickable: Story = {
264
+ args: {
265
+ rowLink: (row) => (row.status === "Declined" ? undefined : `/applicants/${row.id}`),
266
+ linkComponent: FakeRouterLink,
267
+ size: "sm",
268
+ },
269
+ render: ClickableRows.render,
270
+ };
@@ -1,5 +1,5 @@
1
1
  <script setup lang="ts" generic="Row extends Record<string, any>">
2
- import { computed, onBeforeUnmount, onMounted, ref, useId, type HTMLAttributes } from 'vue'
2
+ import { computed, onBeforeUnmount, onMounted, ref, useId, type Component, type HTMLAttributes } from 'vue'
3
3
  import { ArrowDown, ArrowUp, ChevronsUpDown } from '@lucide/vue'
4
4
  import { cn } from '../../lib/cn'
5
5
  import EmptyState from '../EmptyState/EmptyState.vue'
@@ -19,6 +19,11 @@ import { nextSort, sortRows, type TableColumn, type TableSort } from './table'
19
19
  * `manualSort` it only reports the sort (`v-model:sort`) so you can sort on
20
20
  * the server.
21
21
  *
22
+ * Clickable rows: `rowLink` gives each row a page to go to. The row-header
23
+ * cell (or the first column) becomes a real link, stretched over the whole
24
+ * row, so the row highlights on hover and a click anywhere on it follows the
25
+ * link. Buttons and links in other cells still work on their own.
26
+ *
22
27
  * Slots: cell-{key}, header-{key}, empty, footer (rows for a `<tfoot>`,
23
28
  * e.g. totals), caption.
24
29
  */
@@ -43,6 +48,14 @@ export interface TableProps<Row = Record<string, any>> {
43
48
  stickyHeader?: boolean
44
49
  /** Don't sort the rows: just update `sort`, e.g. to sort on the server. */
45
50
  manualSort?: boolean
51
+ /**
52
+ * Where each row goes when clicked: passed as `to` to `linkComponent`
53
+ * (e.g. a route), or as `href` without one. Return undefined for rows that
54
+ * don't go anywhere.
55
+ */
56
+ rowLink?: (row: Row, index: number) => unknown
57
+ /** Component for row links, e.g. RouterLink. It receives `to`. Default a plain `<a href>`. */
58
+ linkComponent?: string | Component
46
59
  class?: HTMLAttributes['class']
47
60
  ui?: {
48
61
  table?: HTMLAttributes['class']
@@ -53,7 +66,8 @@ export interface TableProps<Row = Record<string, any>> {
53
66
 
54
67
  const props = withDefaults(defineProps<TableProps<Row>>(), {
55
68
  emptyText: 'Nothing to show.',
56
- size: 'md'
69
+ size: 'md',
70
+ linkComponent: 'a'
57
71
  })
58
72
 
59
73
  const sort = defineModel<TableSort | null>('sort', { default: null })
@@ -86,6 +100,30 @@ function ariaSort(column: TableColumn<Row>) {
86
100
  return sort.value.direction === 'asc' ? 'ascending' : 'descending'
87
101
  }
88
102
 
103
+ // The column whose cell holds the row's link: the row header, else the first.
104
+ const linkColumn = computed(() => (props.columns.find((c) => c.rowHeader) ?? props.columns[0])?.key)
105
+
106
+ function linkFor(row: Row, index: number) {
107
+ const target = props.rowLink?.(row, index)
108
+ if (target === undefined || target === null || target === '') return undefined
109
+ return props.linkComponent === 'a' ? { href: target as string } : { to: target }
110
+ }
111
+
112
+ // The link's ::after covers the row (the row is `relative`), so a click
113
+ // anywhere follows it. Controls in other cells are lifted above it, so they
114
+ // still get their own clicks. The focus ring is drawn on the ::after, round
115
+ // the whole row, just inside its edge.
116
+ const rowLinkClass = [
117
+ 'text-inherit no-underline outline-none group-hover/row:underline underline-offset-2',
118
+ 'after:absolute after:inset-0 after:outline-2 after:-outline-offset-2 after:outline-transparent',
119
+ 'focus-visible:after:outline-focus forced-colors:not-focus-visible:after:outline-0'
120
+ ]
121
+ const linkedRowClass = [
122
+ 'relative cursor-pointer hover:bg-black/5',
123
+ '[&_:is(a,button,input,select,textarea,label,[tabindex]):not([data-row-link])]:relative',
124
+ '[&_:is(a,button,input,select,textarea,label,[tabindex]):not([data-row-link])]:z-1'
125
+ ]
126
+
89
127
  const cellPadding = computed(() => (props.size === 'sm' ? 'px-4 py-2' : 'px-4 py-3'))
90
128
 
91
129
  function columnClasses(column: TableColumn<Row>) {
@@ -183,7 +221,11 @@ onBeforeUnmount(() => observer?.disconnect())
183
221
  </slot>
184
222
  </td>
185
223
  </tr>
186
- <tr v-for="(row, index) in shownRows" :key="keyOf(row, index)" :class="cn('group/row', ui?.row)">
224
+ <tr
225
+ v-for="(row, index) in shownRows"
226
+ :key="keyOf(row, index)"
227
+ :class="cn('group/row', linkFor(row, index) && linkedRowClass, ui?.row)"
228
+ >
187
229
  <component
188
230
  :is="column.rowHeader ? 'th' : 'td'"
189
231
  v-for="column in columns"
@@ -199,7 +241,18 @@ onBeforeUnmount(() => observer?.disconnect())
199
241
  )
200
242
  "
201
243
  >
202
- <slot :name="`cell-${column.key}`" :row="row" :value="row[column.key]" :index="index">
244
+ <component
245
+ :is="linkComponent"
246
+ v-if="column.key === linkColumn && linkFor(row, index)"
247
+ v-bind="linkFor(row, index)"
248
+ data-row-link
249
+ :class="rowLinkClass"
250
+ >
251
+ <slot :name="`cell-${column.key}`" :row="row" :value="row[column.key]" :index="index">
252
+ {{ row[column.key] ?? '' }}
253
+ </slot>
254
+ </component>
255
+ <slot v-else :name="`cell-${column.key}`" :row="row" :value="row[column.key]" :index="index">
203
256
  {{ row[column.key] ?? '' }}
204
257
  </slot>
205
258
  </component>