diff --git a/e2e/tooltip.e2e.ts b/e2e/tooltip.e2e.ts new file mode 100644 index 00000000..5d5a7a98 --- /dev/null +++ b/e2e/tooltip.e2e.ts @@ -0,0 +1,23 @@ +import { expect, test } from "@playwright/test"; + +const testUrl = "/components/tooltip"; + +test("Should render tooltip", async ({ page }) => { + await page.goto(testUrl); + await page.setViewportSize({ width: 400, height: 720 }); + + const showcase = page.getByTestId("showcase"); + await showcase.scrollIntoViewIfNeeded(); + + await expect( + showcase.getByTestId("tooltip-component").nth(1).locator(".tooltip"), + ).not.toBeVisible(); + + showcase.locator("button.secondary").nth(1).hover(); + + await expect( + showcase.getByTestId("tooltip-component").nth(1).locator(".tooltip"), + ).toBeVisible(); + + await expect(page).toHaveScreenshot(); +}); diff --git a/e2e/tooltip.e2e.ts-snapshots/Should-render-tooltip-1-Google-Chrome-linux.png b/e2e/tooltip.e2e.ts-snapshots/Should-render-tooltip-1-Google-Chrome-linux.png new file mode 100644 index 00000000..1292f4cf Binary files /dev/null and b/e2e/tooltip.e2e.ts-snapshots/Should-render-tooltip-1-Google-Chrome-linux.png differ diff --git a/src/docs/constants/docs.constants.ts b/src/docs/constants/docs.constants.ts index e8bf0437..7b48a1f0 100644 --- a/src/docs/constants/docs.constants.ts +++ b/src/docs/constants/docs.constants.ts @@ -253,6 +253,12 @@ export const COMPONENT_ROUTES: ComponentRoute[] = [ description: "An opinionated theme toggle.", }, + { + path: "/components/tooltip", + title: "Tooltip", + description: "Tooltips provide extra information on hover or tap.", + }, + { path: "/components/toggle", title: "Toggle", diff --git a/src/lib/components/Tooltip.svelte b/src/lib/components/Tooltip.svelte new file mode 100644 index 00000000..6d105923 --- /dev/null +++ b/src/lib/components/Tooltip.svelte @@ -0,0 +1,170 @@ + + + + +
+
+ +
+ +
+ + diff --git a/src/routes/(split)/components/tooltip/+page.md b/src/routes/(split)/components/tooltip/+page.md new file mode 100644 index 00000000..6c95a72f --- /dev/null +++ b/src/routes/(split)/components/tooltip/+page.md @@ -0,0 +1,110 @@ + + +# Tooltip + +Used to provide extra information, often about why a button is disabled, on +hover or tap over the target element. + +```javascript + + + +``` + +## Properties + +| Property | Description | Type | Default | +| ------------------- | --------------------------------------------------------------- | --------- | --------------------- | +| `id` | Used to link the target to the tooltip via `aria-describedby` | `string` | | +| `testId` | Add a `data-tid` attribute to the DOM, useful for test purpose. | `string` | `"tooltip-component"` | +| `text` | The text displayed in the tooltip. | `string` | `""` | +| `noWrap` | Whether to prevent the tooltip text from taking mulitple lines. | `boolean` | `false` | +| `top` | Whether to prevent the tooltip text from taking mulitple lines. | `boolean` | `false` | +| `center` | Whether to ignore overflow logic an just center align instead. | `boolean` | `false` | +| `containerSelector` | Used to query for the container used to determine overflow. | `string` | `"main"` | + +## Slots + +| Slot name | Description | +| ------------ | -------------------------- | +| Default slot | The target of the tooltip. | + +## Showcase + +The tooltips will appear when the buttons are hovered or tapped. + +
+
+ + + + + + + + + +
+
+ + + + + + + + + +
+
+ + diff --git a/src/tests/lib/components/Tooltip.spec.ts b/src/tests/lib/components/Tooltip.spec.ts new file mode 100644 index 00000000..474117ed --- /dev/null +++ b/src/tests/lib/components/Tooltip.spec.ts @@ -0,0 +1,21 @@ +import { render } from "@testing-library/svelte"; +import TooltipTest from "./TooltipTest.svelte"; + +describe("Tooltip", () => { + it("should render target content", () => { + const { container } = render(TooltipTest); + + const element: HTMLParagraphElement | null = container.querySelector("p"); + + expect(element).toBeInTheDocument(); + expect(element?.innerHTML).toBe("content"); + }); + + it("should render aria-describedby and relevant id", () => { + const { container } = render(TooltipTest); + expect( + container.querySelector("[aria-describedby='tid']"), + ).toBeInTheDocument(); + expect(container.querySelector("[id='tid']")).toBeInTheDocument(); + }); +}); diff --git a/src/tests/lib/components/TooltipTest.svelte b/src/tests/lib/components/TooltipTest.svelte new file mode 100644 index 00000000..82757cf8 --- /dev/null +++ b/src/tests/lib/components/TooltipTest.svelte @@ -0,0 +1,7 @@ + + + +

content

+