diff --git a/apps/lite/e2e/tests/screenshots.spec.ts b/apps/lite/e2e/tests/screenshots.spec.ts index 0f75afb01b2..ea466de7f6d 100644 --- a/apps/lite/e2e/tests/screenshots.spec.ts +++ b/apps/lite/e2e/tests/screenshots.spec.ts @@ -63,7 +63,7 @@ test.describe("screenshots", () => { .getByRole("treeitem", { name: "C", exact: true }) .getByTitle("C", { exact: true }) .click(); - await appWindow.getByRole("button", { name: "Pull Request", exact: true }).click(); + await appWindow.getByRole("tab", { name: "Pull Request", exact: true }).click(); await expect(appWindow.getByPlaceholder("PR title")).toBeVisible(); await shoot(appWindow, "pr-form", "#details-panel"); }); diff --git a/apps/lite/e2e/tests/workspace-reviews.spec.ts b/apps/lite/e2e/tests/workspace-reviews.spec.ts index dcdaa8e7965..add05061abd 100644 --- a/apps/lite/e2e/tests/workspace-reviews.spec.ts +++ b/apps/lite/e2e/tests/workspace-reviews.spec.ts @@ -97,7 +97,7 @@ test.beforeEach(async ({ appWindow, electronApp, reviewState }) => { const selectBranch = (page: Page, name: string) => page.getByRole("treeitem", { name, exact: true }).getByTitle(name, { exact: true }).click(); -const branchTabs = (page: Page) => page.getByRole("group", { name: "Branch tab", exact: true }); +const branchTabs = (page: Page) => page.getByRole("tablist", { name: "Branch tab", exact: true }); test("shows PR titles and labels on workspace branches without hover shifts", async ({ appWindow, @@ -172,14 +172,14 @@ for (const { reviewState, defaultTab, showCreateButton } of [ exact: true, }); await expect(createPullRequest).toBeVisible({ visible: showCreateButton }); - await expect(tabs.getByRole("button", { name: defaultTab, pressed: true })).toBeVisible(); + await expect(tabs.getByRole("tab", { name: defaultTab, selected: true })).toBeVisible(); await expect(appWindow.getByPlaceholder("PR title")).toBeHidden(); const chosenTab = defaultTab === "Diff" ? "Pull Request" : "Diff"; - await tabs.getByRole("button", { name: chosenTab, exact: true }).click(); + await tabs.getByRole("tab", { name: chosenTab, exact: true }).click(); await selectBranch(appWindow, "B"); await selectBranch(appWindow, "C"); - await expect(tabs.getByRole("button", { name: chosenTab, pressed: true })).toBeVisible(); + await expect(tabs.getByRole("tab", { name: chosenTab, selected: true })).toBeVisible(); }); }); } diff --git a/apps/lite/scripts/check-tokens.mjs b/apps/lite/scripts/check-tokens.mjs index 87d6fb7eb64..b01adc049a3 100644 --- a/apps/lite/scripts/check-tokens.mjs +++ b/apps/lite/scripts/check-tokens.mjs @@ -46,6 +46,11 @@ const KNOWN_RUNTIME = new Map([ // Base UI's scroll area writes the thumb's length on its scrollbar element. ["--scroll-area-thumb-height", "@base-ui/react scroll area scrollbar"], ["--scroll-area-thumb-width", "@base-ui/react scroll area scrollbar"], + // Base UI's tabs indicator carries the selected tab's box. + ["--active-tab-top", "@base-ui/react tabs indicator"], + ["--active-tab-height", "@base-ui/react tabs indicator"], + ["--active-tab-width", "@base-ui/react tabs indicator"], + ["--active-tab-left", "@base-ui/react tabs indicator"], // Shiki's dual-theme output carries both themes as inline style. ["--shiki-light", "shiki dual-theme inline style"], ["--shiki-dark", "shiki dual-theme inline style"], diff --git a/apps/lite/ui/src/routes/project/$id/workspace/Details.module.css b/apps/lite/ui/src/routes/project/$id/workspace/Details.module.css index fe5f4239471..27a93475abe 100644 --- a/apps/lite/ui/src/routes/project/$id/workspace/Details.module.css +++ b/apps/lite/ui/src/routes/project/$id/workspace/Details.module.css @@ -19,6 +19,8 @@ flex-direction: column; /* Symmetric so the header actions share a right edge with the content columns below, which all inset by the same token. */ + /* 12px below: the tabs' underline hangs 8px under them and is 4px thick, + so it sits on the header's bottom edge. */ padding-inline: var(--panel-padding-inline); padding-block: var(--panel-padding-block) 12px; /* The header draws its own bottom edge, so the tab row reads as a strip diff --git a/apps/lite/ui/src/routes/project/$id/workspace/Details.tsx b/apps/lite/ui/src/routes/project/$id/workspace/Details.tsx index 3f97d6f24c5..bce8c7056d9 100644 --- a/apps/lite/ui/src/routes/project/$id/workspace/Details.tsx +++ b/apps/lite/ui/src/routes/project/$id/workspace/Details.tsx @@ -74,6 +74,7 @@ import { projectSlice } from "#ui/projects/state.ts"; import { interfaceSlice } from "#ui/interface/state.ts"; import { Badge } from "@gitbutler/ui-react/Badge.tsx"; import { Button, getButtonClassName } from "@gitbutler/ui-react/Button.tsx"; +import { Tab, Tabs } from "@gitbutler/ui-react/Tabs.tsx"; import { Icon } from "@gitbutler/ui-react/Icon.tsx"; import { Tooltip } from "@gitbutler/ui-react/Tooltip.tsx"; import { useCopied } from "#ui/components/useCopied.ts"; @@ -3141,23 +3142,19 @@ const BranchTabToggle: FC<{ prDisabled?: boolean; className?: string; }> = ({ branchTab, setBranchTab, prDisabled = false, className }) => ( - } - value={[branchTab]} - onValueChange={(value: Array) => { - const head = value[0]; - if (head === undefined) return; - setBranchTab(head); - }} + setBranchTab(value)} aria-label="Branch tab" > - } value={"diff" satisfies BranchTab}> + }> Diff - - } value={"pr" satisfies BranchTab} disabled={prDisabled}> + + } disabled={prDisabled}> {prDisabled ? "No pull request" : "Pull Request"} - - + + ); /** `[` and `]` step between a branch's tabs; with two of them, either key toggles. */ diff --git a/packages/ui-react/src/Tabs.module.css b/packages/ui-react/src/Tabs.module.css new file mode 100644 index 00000000000..171bf217ea4 --- /dev/null +++ b/packages/ui-react/src/Tabs.module.css @@ -0,0 +1,82 @@ +.list { + /* The tab's inset, which the underline keeps too, so it runs under the tab's content. */ + --tab-padding-inline: 10px; + + display: flex; + position: relative; + column-gap: 2px; + align-items: center; +} + +.tab { + display: inline-flex; + column-gap: 6px; + align-items: center; + height: var(--size-control); + padding-inline: var(--tab-padding-inline); + border-radius: var(--radius-control); + color: var(--text-1); + white-space: nowrap; + cursor: var(--control-cursor); + transition: background-color var(--transition-fast); + + &:hover:not([data-active], [data-disabled]) { + background-color: var(--bg-hover); + } + + /* Unavailable for now: it stays faded under the pointer, and its label says why. */ + &[data-disabled] { + cursor: default; + } + + &:focus-visible { + outline: var(--focus-ring); + } +} + +.icon { + display: flex; + opacity: 0.5; + + /* Only on a tab that isn't selected: the selected one doesn't answer the pointer. */ + .tab:hover:not([data-active], [data-disabled]) & { + opacity: 0.8; + } + + .tab[data-active] & { + opacity: 0.7; + } +} + +/* The label and its shortcut fade together. */ +.label { + display: inline-flex; + column-gap: 3px; + align-items: center; + opacity: 0.5; + + :is(.tab:hover:not([data-disabled]), .tab[data-active]) & { + opacity: 1; + } +} + +/* Under the selected tab's content, 8px below it; moves and resizes to the next as the + selection moves. */ +.indicator { + position: absolute; + top: calc(var(--active-tab-top) + var(--active-tab-height) + 8px); + left: 0; + width: calc(var(--active-tab-width) - 2 * var(--tab-padding-inline)); + height: 4px; + translate: calc(var(--active-tab-left) + var(--tab-padding-inline)) 0; + background-color: var(--fill-gray-bg); + transition: + translate var(--transition-medium) ease-out, + width var(--transition-medium) ease-out; +} + +@media (prefers-reduced-motion: reduce) { + .indicator { + transition: none; + } +} diff --git a/packages/ui-react/src/Tabs.stories.tsx b/packages/ui-react/src/Tabs.stories.tsx new file mode 100644 index 00000000000..f69d924ab9b --- /dev/null +++ b/packages/ui-react/src/Tabs.stories.tsx @@ -0,0 +1,101 @@ +import preview from "#storybook/preview"; +import { useState } from "react"; +import { Icon } from "./Icon.tsx"; +import { Tab, Tabs } from "./Tabs.tsx"; + +const meta = preview.meta({ + component: Tabs, + parameters: { + design: { + type: "figma", + url: "https://www.figma.com/design/cqdnAotT8n9op8WGYLOHg4/%E2%9A%9B%EF%B8%8F-Lite-Core?node-id=2374-4526", + }, + }, + decorators: [ + (Story) => ( +
+ +
+ ), + ], +}); + +/** Tabs that switch a pane's contents; the underline follows the selected one. */ +export const Default = meta.story({ + render: () => ( + + Diff + Pull Request + Sessions + Activity + + ), +}); + +/** With an `icon` before each label and a `count` after the ones that hold things. */ +export const IconsAndCounts = meta.story({ + render: () => ( + + } count={3}> + Changes + + }> + Pull Request + + } count={2}> + Sessions + + }> + Activity + + + ), +}); + +/** With `kbd`, the shortcut that selects each tab, faint after its label. */ +export const Shortcuts = meta.story({ + render: () => ( + + + Diff + + + Pull Request + + + Activity + + + ), +}); + +/** + * In a pane's header, controlled by the pane. The header leaves 12px below the row, the underline's + * 8px gap and its 4px, so the underline sits on the divider rather than across it. + */ +export const InAHeader = meta.story({ + render: function Render() { + const [tab, setTab] = useState("changes"); + return ( +
+
+ + + Changes + + Activity + +
+

+ {tab === "changes" ? "Three changed files." : "Nothing happened yet."} +

+
+ ); + }, +}); diff --git a/packages/ui-react/src/Tabs.tsx b/packages/ui-react/src/Tabs.tsx new file mode 100644 index 00000000000..688f29e0a51 --- /dev/null +++ b/packages/ui-react/src/Tabs.tsx @@ -0,0 +1,59 @@ +import { Tabs as BaseTabs } from "@base-ui/react"; +import type { HotkeySequence } from "@tanstack/react-hotkeys"; +import type { FC, ReactNode } from "react"; +import { Badge } from "./Badge.tsx"; +import { classes } from "./classes.ts"; +import { Kbd } from "./Kbd.tsx"; +import styles from "./Tabs.module.css"; + +/** + * A row of {@link Tab}s that switch what a pane shows, one at a time, with an underline under the + * selected one that slides to the next as the selection moves. + * + * - Controlled with `value` and `onValueChange`, or left to itself with `defaultValue`. + * - The arrow keys move between the tabs, as a tab list does. + * - The underline runs under the selected tab's content and hangs outside the row, 8px below it + * and 4px thick, so the header holding the tabs leaves 12px below them: the underline then + * sits on the header's divider. + * + * @public + * @import import { Tabs, Tab } from "@gitbutler/ui-react/Tabs.tsx"; + */ +export const Tabs: FC< + Omit & { + className?: string; + "aria-label"?: string; + } +> = ({ className, children, "aria-label": ariaLabel, ...props }) => ( + + + {children} + + + +); + +/** + * One tab in {@link Tabs}: its label, an optional `icon` before it and an optional `count` after + * it, for how many things the tab holds. `kbd` names the shortcut that selects it, faint after + * the label. Faded until it is hovered or selected. + * + * @public + */ +export const Tab: FC< + Omit & { + className?: string; + icon?: ReactNode; + count?: number; + kbd?: string | HotkeySequence; + } +> = ({ className, icon, count, kbd, children, ...props }) => ( + + {icon !== undefined && {icon}} + + {children} + {kbd !== undefined && } + + {count !== undefined && {count}} + +);