# kanok-ui (@purpurabog/ui 2.3.0): the full documentation > Carbon Design System components for any web app: Lit web components (``, any framework), React wrappers, unstyled React parts (headless) and React + Tailwind components you copy in with shadcn. Themes white / g10 / g90 / g100, layers, Thai and English. npm package `@purpurabog/ui`; shadcn registry namespace `@kanok-ui`. Rules for writing code with it: use these components instead of raw HTML controls; colour and space with the `--ui-*` tokens (never hard-coded colours); restyle with `::part()` and tokens, not the shadow DOM; nest `` (or React `Layer`) for surfaces on surfaces. Each page below has an Installation section (npm and shadcn commands) and an API section (attributes, properties, events, slots, parts). The design tokens and rules are in [DESIGN.md](https://parntengza.github.io/kanok-ui-docs/DESIGN.md) (also in the package: `node_modules/@purpurabog/ui/DESIGN.md`). Thai versions of every page are under `th/`. # @purpurabog/ui Components that follow IBM's Carbon Design System, made for forms in Thai and English: fields, lists, dates (Buddhist era), overlays, data tables and the UI shell. This site is built with them. The same components come in three forms. Pick the one that fits your app: | You build with | Use | You get | |---|---|---| | Any framework, or plain HTML | Lit web components (``) | Carbon's look and behaviour, styled with tokens and `::part()` | | React + Tailwind | Components copied into your app with shadcn (`npx shadcn add`) | Carbon's look as Tailwind classes you can edit line by line | | React, your own design | [Unstyled parts](https://parntengza.github.io/kanok-ui-docs/en/headless/) (`@purpurabog/ui/headless`) | Carbon's keyboard, focus and ARIA with no classes at all | All three share one core, so the behaviour is the same everywhere: every component is tested against Carbon in Chrome and WebKit, with axe and with the keyboard. ## Try it ```html ``` Change the theme or the language in the settings at the top right: the components follow the page's `data-theme` and `lang`. ## What next - [Install the package](https://parntengza.github.io/kanok-ui-docs/en/installation/) or copy components with shadcn. - Browse every element, its attributes, slots, parts and events in the Storybook. --- # Installation The package is public on npm. Pick the way that fits your app; you can mix them. ## Choose a style The components come in two styles. Both use the same JavaScript: a style is only a stylesheet. | Style | Load | On the page | | --- | --- | --- | | **Carbon** (the default) | `tokens.css` | nothing to add | | **Kanok** (our own design) | `tokens.css`, then `kanok.css` | `data-design="kanok"` on `` or on your app's root element | | **Both**, switchable | the same two files | set or remove `data-design` at run time | ```html ``` Kanok uses the Bai Jamjuree font (Thai and Latin, SIL Open Font License). Install it with `npm i @fontsource/bai-jamjuree`, then import the weights 400, 500, 600 and 700 (`import '@fontsource/bai-jamjuree/400.css';` and so on). To see the whole site in Kanok, open **Settings** (the gear in the header) and choose **Kanok** under Style, or share a link ending in `?design=kanok`. More in [Kanok design](https://parntengza.github.io/kanok-ui-docs/en/kanok/). ## Web components (any framework) ```bash npm i @purpurabog/ui lit ``` Load the design tokens once per page, then the components you use: ```html ``` ### React React wrappers with typed props and events come with the package. They need `@lit/react` too (an optional peer dependency, only for `@purpurabog/ui/react`): ```bash npm i @purpurabog/ui lit @lit/react ``` Load the tokens once (`import '@purpurabog/ui/tokens.css';`), then import the wrappers you use from `@purpurabog/ui/react`. ## React + Tailwind with shadcn Copy a component into your app and edit its classes. Its behaviour (keyboard, focus, ARIA) comes from `@purpurabog/ui/headless`, which the CLI installs for you. 1. Your app needs Tailwind v4 and a `components.json`. If you already use shadcn, add only the `registries` part: ```json { "$schema": "https://ui.shadcn.com/schema.json", "style": "new-york", "rsc": false, "tsx": true, "tailwind": { "config": "", "css": "src/index.css", "baseColor": "neutral", "cssVariables": true, "prefix": "" }, "aliases": { "components": "@/components", "utils": "@/lib/utils", "ui": "@/components/ui", "lib": "@/lib", "hooks": "@/hooks" }, "registries": { "@kanok-ui": { "url": "https://cdn.jsdelivr.net/npm/@purpurabog/ui@latest/registry/{name}.json" } } } ``` Pin the version in the URL (`@purpurabog/ui@0.82.0` instead of `@latest`) so every copy in your app comes from the same release. 2. Add components: ```bash npx shadcn@4 add @kanok-ui/tw-select @kanok-ui/tw-button ``` 3. Import the tokens and the theme after Tailwind in your main CSS: ```css @import 'tailwindcss'; @import './components/ui/primitives/tokens.css'; @import './components/ui/react-tw/theme.css'; ``` The files land in `components/ui/` (under `src/` when your app has one). ## Unstyled React (headless) For your own design system: the behaviour without a single class. ```bash npm i @purpurabog/ui react react-dom ``` State shows as `data-*` attributes (`data-open`, `data-selected`, `data-highlighted` …) to style on. The parts have their own section: start at [Headless](https://parntengza.github.io/kanok-ui-docs/en/headless/). ## Use your first component The same dropdown as a web component, with the React wrapper and with React + Tailwind. Every example on this site comes in these tabs: pick yours once and the other pages follow. The headless version is in [Headless](https://parntengza.github.io/kanok-ui-docs/en/headless/#your-first-component). HTML: ```html ``` React: ```tsx import { UiDropdown } from '@purpurabog/ui/react'; ``` React + Tailwind: ```tsx // npx shadcn@4 add @kanok-ui/tw-dropdown import { Dropdown } from '@/components/ui/react-tw/dropdown'; const types = [ { value: 'sale', label: 'Sale' }, { value: 'lease', label: 'Lease' }, ]; ``` `lang` on the element or any ancestor sets the language of the built-in text of the web components and the React wrappers (Thai and English are included). --- # Migrating to 1.0 1.0 replaces the data-driven web component `` with a family of elements that follow Carbon web components (`cds-table`): ``, ``, ``, ``, ``, ``, `` and the toolbar, batch-action and skeleton elements around them. This page lists what to change. ## What changed and why - **Markup instead of data.** `` took `headers` and `rows` as properties and drew the table itself. `` is written as elements, as Carbon's `cds-table` is: each header, row and cell is an element you write or render. What a cell shows is whatever you put in it, so `renderCell` is gone. - **Carbon web components names.** Attributes, events, slots and parts now follow `cds-table`: `is-sortable`, `is-selectable`, `use-zebra-styles`, `ui-table-header-cell-sort`, `ui-table-row-selected` and so on. Features Carbon doesn't have (paging, column settings, pinned and resized columns, CSV, inline editing, loading and empty states) keep working with the same behaviour and get names in the same style. - **The toolbar is elements too.** The search, the batch actions, the Columns button and the Export CSV button are ``, ``, `` and `` inside a ``, instead of switches on the table. - **New entry point.** Import `@purpurabog/ui/table` instead of `@purpurabog/ui/data-table`. **Unchanged:** the React + Tailwind `DataTable` (`tw-data-table`, data-driven with `headers` and `rows`, including `DataTableSkeleton`) and the headless `useDataTable` hook and its parts. Code that uses them needs no change. ## Before and after Before: ```html Archive ``` After: ```html Contracts Archive Contract Amount (THB) Service agreement 1,250,000 NDA 0 ``` ## Attributes and properties | `` | 1.0 | |---|---| | `headers` (`{ key, header }[]`) | ``s in a `` › ``; `key` → `column`, `header` → the cell's content | | `rows` (`{ id, … }[]`) | ``s of ``s in a ``; keep the id yourself (`data-id`, a React `key`) | | `renderCell(row, header)` | Write the content into the `` | | `size` | `size` (unchanged) | | `zebra` | `use-zebra-styles` | | `static-width` | `use-static-width` | | `sortable` | `is-sortable` on the table (every header) or on single headers | | `sort-key` + `sort-direction` | `sort-direction` on that `` | | `manual-sorting` | `preventDefault()` on `ui-table-header-cell-sort`, then sort the data and set `sort-direction` | | `sortRow(a, b, context)` | `sort-value` on cells; `customSortRow(lhs, rhs, collator)` on the table compares two cells' values | | `heading` | `` | | `description` | `` | | `label` | `aria-label` on `` | | `selectable` | `is-selectable` | | `radio` | `radio` (unchanged) | | `selectedIds` | `selected` on ``; `selectedRows` (the row elements) on the table | | `searchable` | `` in `` | | `search-persistent` | `persistent` on `` | | `search-label`, `search-placeholder` | `label`, `placeholder` on `` | | `filter-text` | No attribute: set the search's `value` and dispatch an `input` event from it | | `filterRows(row, text, headers)` (true keeps the row) | `filterRows(rowText, searchString)` on the table — **true now leaves the row out**, as in Carbon | | `manual-filtering` | `filterRows = () => false`, and listen for `input` on the search | | `renderExpandedRow(row)` | `expandable` on the table and a `` right after the row | | `expandedIds` | `expanded` on `` | | `expand-all` | `batch-expansion` (with `expandable`) | | `sticky-header` | `sticky-header` (unchanged) | | `resizable` | `resizable` on the table (every header) or on single headers | | `columnWidths` | `width` on each header; `columnWidths` on the table is now read-only | | `loading`, `loading-rows` | Unchanged | | `page-size`, `page`, `page-sizes` | Unchanged | | `total-selected` | `selected-rows-count` on `` (set it in your `ui-table-row-selected` listener) | | `total-count` | `total-rows-count` on `` | | `column-settings` | `` in `` | | `hiddenColumns` | `column-hidden` on headers, `setColumnHidden(column, hidden)` and `resetColumns()`; `hiddenColumns` on the table is now read-only | | `csv-export` | `` in `` | | `csv-file-name` | `file-name` on `` | | Host `data-has-selection` | `selected` on `` while any row is chosen (`ui-table:has(ui-table-header-row[selected])`) | Header fields: | `DataTableHeader` | `` | |---|---| | `key` | `column` | | `header` | The cell's content | | `sortable: false` | Leave `is-sortable` off the table and put it on the headers that sort | | `align` | No attribute: style the cells, e.g. `style="text-align: end"` on `` | | `editable`, `options` | `editable`, `options` (a property, or JSON in the attribute) | | `validate(value, row)` | `validate(value, cell)` (a property) | | `resizable: false` | Leave `resizable` off the table and put it on the headers that resize | | `width`, `minWidth`, `maxWidth` | `width`, `min-width`, `max-width` | | `hideable: false` | `hideable="false"` | | `hidden: true` | `column-hidden` | | `pinned` | `pinned` | Row fields: `id` is yours to keep (`data-id`); `disabled: true` → `disabled` on ``. ## Methods | `` | 1.0 | |---|---| | `toCsv({ scope })`, `downloadCsv({ scope, fileName })` | Unchanged on `` | | `focus()` | Unchanged | | `selectedRows` | `selectedRows` returns the `` elements | | `sortBy(key, direction)` | None: click the header, or sort the data yourself and set `sort-direction` | | `filteredRows`, `visibleRows`, `shownRows` | None: `table.querySelectorAll('ui-table-row:not([filtered])')`; rows on other pages also have `paged-out` | | `pageCount` | None: `Math.ceil(rows / pageSize)` | | `visibleHeaders` | `table.columns.filter((header) => !header.columnHidden)` | ## Events | `` | 1.0 | |---|---| | `ui-sort` `{ key, direction }` | `ui-table-header-cell-sort` `{ oldSortDirection, sortDirection }` from the header, cancelable, before the rows move; `ui-table-sorted` `{ sortedHeader }` after | | `ui-select` `{ selected, rows }` | `ui-table-row-selected` `{ selectedRow, selectedRows }`; `ui-table-row-all-selected` `{ selectedRows }` after select-all or Cancel | | `ui-filter` `{ value }` | `ui-table-filtered` `{ unfilteredRows }`; `input` on `` for the text | | `ui-cell-edit` `{ rowId, key, value, previous, row }`, after the save | `ui-table-cell-edit` `{ row, cell, column, value, previous }` from the cell, cancelable, before the value is written | | `ui-page-change` | `ui-table-page-change` (same detail) | | `ui-select-all` `{ totalCount }` | `ui-table-batch-actions-select-all-clicked` from `` | | `ui-column-resize` `{ key, width, widths }` | `ui-table-column-resize` `{ column, width }` from the header; read `columnWidths` on the table for all | | `ui-expand` `{ expandedIds, expanded, id }` | `ui-table-row-expando-toggled` `{ expanded }` from the row (cancelable `ui-table-row-expando-beingtoggled` before) | | `ui-csv-export` `{ csv, rows, fileName }` | `ui-table-csv-export` `{ csv, rows, fileName }` from ``, cancelable; `rows` are now the row elements | | `ui-columns-change` `{ hiddenColumns, key, hidden }` | `ui-table-columns-change` `{ hiddenColumns, column, hidden }` | ## Slots | `` | 1.0 | |---|---| | `toolbar` | Your controls go in ``, inside `` | | `batch-actions` | The children of `` | | `empty`, `no-results` | Unchanged | | — | New: `title`, `description` | ## CSS parts and custom property | `` | 1.0 | |---|---| | `base` | None: style `ui-table` | | `header`, `content`, `table`, `pagination`, `empty` | Unchanged on `ui-table` | | `title`, `description` | Style `ui-table-header-title`, `ui-table-header-description` | | `toolbar`, `toolbar-content`, `search` | Style `ui-table-toolbar`, `ui-table-toolbar-content`, `ui-table-toolbar-search` | | `batch-actions` | Style `ui-table-batch-actions` | | `batch-summary`, `cancel-button`, `select-all-button` | `ui-table-batch-actions::part(…)`, same names | | `sort-button`, `resizer` | `ui-table-header-cell::part(sort-button)`, `::part(resizer)` | | `columns-button`, `columns-panel` | `ui-table-column-settings::part(button)`, `::part(panel)` | | `export-button` | `ui-table-csv-export::part(button)` | | `expand-button` | `ui-table-row::part(expand-button)` (`ui-table-header-row` for expand-all) | | `expanded-row` | `ui-table-expanded-row::part(cell)` | | `--ui-data-table-max-height` | `--ui-table-max-height` | React + Tailwind's `DataTable` still reads `--ui-data-table-max-height`. ## Skeleton `` (`UiDataTableSkeleton`) becomes `` (`UiTableSkeleton`), as Carbon's `cds-table-skeleton`: `headers` is a list of strings instead of `{ header }` objects, `hide-header` becomes `show-header="false"` and `hide-toolbar` becomes `show-toolbar="false"`. `row-count`, `column-count` and `zebra` are unchanged; there is no `size`. ## Screen reader announcements `` said "Sorted by Contract, ascending" after a sort and "3 of 12 rows" when typing paused. `` says only "Loading data" while `loading`, as Carbon's table says nothing; the batch bar's count is still a status. The React + Tailwind `DataTable` keeps both announcements. ## React `UiDataTable` and `UiDataTableSkeleton` are gone from `@purpurabog/ui/react`. Pick one of: - **The `UiTable*` wrappers** (`UiTable`, `UiTableHead`, `UiTableHeaderRow`, `UiTableHeaderCell`, `UiTableBody`, `UiTableRow`, `UiTableCell`, `UiTableExpandedRow`, `UiTableHeaderTitle`, `UiTableHeaderDescription`, `UiTableToolbar`, `UiTableToolbarContent`, `UiTableToolbarSearch`, `UiTableBatchActions`, `UiTableColumnSettings`, `UiTableCsvExport`, `UiTableSkeleton`): map your data into them. Events are `onUiTable…` props, such as `onUiTableRowSelected`. - **The React + Tailwind `DataTable`**: still data-driven with `headers` and `rows`, closest to the old `UiDataTable` props. The table sorts by moving its rows in the DOM, and saves an edit by writing into the cell. Rows React renders belong to React, so sort and save through your state: ```tsx { e.preventDefault(); // React writes the value const { row, column, value } = e.detail; setRows((all) => all.map((r) => (r.id === row.dataset.id ? { ...r, [column]: value } : r))); }} > {headers.map((h) => ( { e.preventDefault(); // React orders the rows setSort({ column: h.key, direction: e.detail.sortDirection }); }} > {h.header} ))} {sortRows(rows, sort).map((row) => ( {headers.map((h) => {row[h.key]})} ))} ``` The same goes for Vue, Angular and any framework that renders the rows. See [Data table](https://parntengza.github.io/kanok-ui-docs/en/data-table/) for every feature in HTML and React. ## Types The `DataTable…` types of the web component (`DataTableHeader`, `DataTableRow`, `DataTableSortDetail` …) are gone from `@purpurabog/ui`; the new ones are `UiTable`, `UiTableRow`, `UiTableHeaderCell` …, `TableSortDirection` and the event details (`TableSortedDetail`, `TableRowSelectedDetail`, `TableCellEditDetail` …). React + Tailwind's own types (`DataTableSort` …) are unchanged. --- # Themes and layers Every color comes from Carbon's four themes, generated from `@carbon/themes` into `tokens.css`. You pick a theme with one attribute, `data-theme`, on the whole page or on any part of it. Inside a theme, `` moves fields and menus one step along Carbon's layering model so they stay visible on the surface below them. | Theme | Look | |---|---| | `white` | Light, white background (the default) | | `g10` | Light, gray 10 background | | `g90` | Dark, gray 90 background | | `g100` | Dark, gray 100 background | `data-theme` and the tokens work the same for the web components, the React wrappers and React + Tailwind, whose classes read the same tokens. Headless parts have no colors of their own. ## Switch the theme of the page Put `data-theme` on ``. Without it the page is `white`. ```html ``` The dark themes (`g90`, `g100`) also set `color-scheme: dark`, so scrollbars and the browser's own controls turn dark too. ## Make a zone dark `data-theme` works on any element: everything inside it uses that theme, the rest of the page doesn't. Zones can nest. A zone only changes the tokens. To paint the zone itself, give it the theme's background and text color: HTML: ```html
``` React: ```tsx
``` React + Tailwind: ```tsx
``` The field inside the layer uses the next layer's color, so it still stands out from the surface it sits on. ## Follow the system setting `data-theme="auto"` shows `white`, or `g100` when the system asks for dark (`prefers-color-scheme: dark`). It's opt-in: Carbon itself never switches on its own. ```html ``` `auto` works on `` or on a zone: an `auto` zone inside another theme still shows `white` or `g100` by the system setting. ## Let people pick light, dark or system `` is an icon content switcher with three choices: light, dark and system. It has no Carbon counterpart. The one under Settings at the top right of this site is this element. HTML: ```html ``` React: ```tsx import { UiThemeSwitcher } from '@purpurabog/ui/react'; ``` React + Tailwind: ```tsx // npx shadcn@4 add @kanok-ui/tw-theme-switcher import { ThemeSwitcher } from '@/components/ui/react-tw/theme-switcher'; ``` To draw your own control in React with no styles, use the `useThemeMode` hook: see [Headless](https://parntengza.github.io/kanok-ui-docs/en/headless/#build-your-own-theme-switcher). What it does: - **Shows the choice on ``.** Light becomes `light-theme` (`white` by default), dark becomes `dark-theme` (`g100` by default), and system follows the OS, also while it changes. - **Remembers it** in `localStorage` under `storage-key` (`ui-theme` by default; `storage-key=""` doesn't remember). - **Starts on** the remembered choice, else `value`, else what `` already shows, else light. - **Keeps every switcher in step**: all switchers on the page (Lit and React) and in other tabs follow each other. | Attribute | Values | Default | |---|---|---| | `value` | `light`, `dark`, `system` | the remembered choice | | `light-theme` | `white`, `g10` | `white` | | `dark-theme` | `g90`, `g100` | `g100` | | `storage-key` | any key; `""` = don't remember | `ui-theme` | | `size` | `sm`, `md`, `lg` | `md` | | `low-contrast` | boolean | off | | `tooltip-placement` | where the icon tooltips show | `top` | | `label` | names the group | "Theme", translated | In React and React + Tailwind the props are camelCase (`lightTheme`, `darkTheme`, `storageKey`, `lowContrast`, `tooltipPlacement`), and the Tailwind switcher takes `aria-label` instead of `label`. On the web component, its parts are `switcher` (the content switcher) and `item` (each choice). ### Save the choice to a user's profile Picking a choice fires `ui-theme-change` with `{ mode, theme }` (`onUiThemeChange` on the React wrapper, `onChange` on the React + Tailwind switcher). Store the mode on your server, and set `value` when the profile loads: the switcher shows it and remembers it. HTML: ```js const switcher = document.querySelector('ui-theme-switcher'); switcher.addEventListener('ui-theme-change', (event) => { const { mode, theme } = event.detail; // e.g. 'dark', 'g100' saveProfile({ theme: mode }); // your own function }); // when the profile arrives switcher.value = profile.theme; ``` React: ```tsx // profile comes from your server; saveProfile is your own function saveProfile({ theme: e.detail.mode })} /> ``` React + Tailwind: ```tsx // with value, the switcher shows what you pass: saveProfile also updates profile.theme saveProfile({ theme: mode })} /> ``` ### Paint the remembered theme on the first frame The switcher only runs once its script has loaded, so on a reload the page would first paint the default theme and then switch. `themeScript()` returns a small script for `` that sets the remembered theme before anything is painted. Render it on the server, or write it into `index.html` in a build step: ```js import { themeScript } from '@purpurabog/ui/core'; const head = ``; ``` Give it the same `storageKey`, `lightTheme` and `darkTheme` as your switcher, whichever of the four it is. When nothing is remembered it does nothing, so your own `` stays the default. On the server `useThemeMode`, and so the React + Tailwind switcher, returns `defaultValue` (or `light`), so add `themeScript()` to `` there too. ## Put fields on a tile: `` Carbon colors surfaces in layers: a field on a gray tile must not be the same gray as the tile. `` is Carbon's Layer. Inside it, components use the theme's next layer set: - field, tile and menu backgrounds, and selected rows - borders and dividers - hover and active fills | `level` | Uses | |---|---| | `0` | The page's own set, as outside any layer | | `1` | The second set | | `2` | The third set (the deepest) | Without `level`, a layer works it out like Carbon's `cds-layer`: a layer outside any other is level `0`, and a nested one is its parent's level + 1, up to `2`. Set `level` to choose one yourself. `with-background` paints the layer with Carbon's layer background, the color its content sits on. HTML: ```html ``` React: ```tsx import { UiLayer, UiTextInput } from '@purpurabog/ui/react'; ``` React + Tailwind: ```tsx import { Layer } from '@/components/ui/react-tw/layer'; import { TextInput } from '@/components/ui/react-tw/text-input'; ``` Carbon React's `` starts at level 1, not 0. The React + Tailwind `Layer` follows Carbon React, so it needs no `level` here. The React wrapper renders `` and starts at 0 like the element: write `level="1"` (`level={1}`) for the same result. ## Use themes inside a shadow root (micro apps) This section is for the web components in a Lit app; for a React + Tailwind app in a shadow root, see [Frameworks](https://parntengza.github.io/kanok-ui-docs/en/frameworks/#run-inside-a-shadow-root-micro-app). Tokens are CSS custom properties, which inherit through shadow roots: a theme on `` reaches every component, wherever it is. The `[data-theme]` rules, though, live in `tokens.css` at document level. If your app renders inside a shadow root of its own and puts `data-theme` on an element in there, load `tokens.css` into that shadow root too. The file also names `:host`, so `data-theme` on your app's host element works like it does on ``: ```ts import { LitElement, html, unsafeCSS } from 'lit'; import tokens from '@purpurabog/ui/tokens.css?inline'; // Vite: the CSS as text import '@purpurabog/ui/text-input'; class ContractsApp extends LitElement { static styles = unsafeCSS(tokens); render() { return html``; } } customElements.define('contracts-app', ContractsApp); ``` ```html ``` The theme switcher only changes ``. In a micro app, set `data-theme` on your own host instead. `` needs nothing extra: its rules are part of the element, so it works in any shadow root. ## What next - [Customize the look](https://parntengza.github.io/kanok-ui-docs/en/customizing/): tokens, `::part()`, states and slots. --- # Kanok design Kanok (กนก, after the flame-like Thai motif *lai kanok*) is the library's own style, next to Carbon. It is meant for internal business tools: clean and calm, surfaces set apart by their fill rather than lines, with a Thai-craft identity in its colours and one recurring detail. Every component keeps the same HTML, JavaScript and accessibility; only the stylesheet changes. ## What it looks like - **Colour.** Blue for actions (`#0068d6`; dark: khram indigo `#5563db`), chad vermilion for danger, cool near-neutral greys on white (theme `paper`) and a violet-warm black (theme `lacquer`). Every colour is generated and checked against WCAG 2.2 contrast when the package is built. - **You are here.** A blue line marks the selected tab, the current page in the header and a chosen table row (`--ui-border-interactive`: the primary on paper, a lighter indigo on lacquer so it stays visible); in the side nav, a tree and vertical tabs the chosen row is tinted, with no bar. - **No lines where a fill will do.** Cards and tiles have no border: a soft grey sets them apart from the page, one step off whatever they sit on, in a layer too. Notifications and toasts have no coloured bar; the icon tells the kind. - **Focus.** One light ring everywhere: solid blue (a light indigo on lacquer), 2px, with rounded corners, shown for the keyboard only. A click on a button, a checkbox or a toggle shows none; Tab does, 2px outside a control and inside a highlighted row. An input box (a text field, a dropdown, a select) shows its focus either way, as Pixiv's soft 4px blue halo outside the box, with no line on its edge; an invalid one has a red halo (Carbon: 2px, square, a click too). - **Shape.** Pill-shaped buttons; 6px corners on fields, a soft grey fill with no edge (or an edge: see [Outlined fields](#outlined-fields)); 12px on cards, tiles and dialogs (a card's footer buttons sit 8px in) (a card clips its image to them); 8px on menus, notifications and code snippets. Tags have small 4px corners, so a status never reads as a button. - **No ink blocks.** Secondary buttons are a soft grey fill with no border. Chosen things are the primary colour, never black: a selected tag, a chosen tile, the slider, the multi-select count. - **Density.** Buttons and fields 40px tall by default (`sm` 32px, `lg` 48px, as Carbon's fields), so a button lines up with the field beside it; menu rows 32px, table rows 40px. The large button sizes stay pills: `xl` is 48px and `2xl` 56px, label centred (Carbon's 64 / 80px with the label at the top). - **Select.** A native select, as in Carbon: it opens the browser's own list, and a phone's own picker. For a list in the menu look, use a dropdown (or set `--ui-select-appearance: base-select` for Chrome 135+'s customizable select). - **Type.** Bai Jamjuree for Thai and Latin, semibold labels and headings, and taller lines so stacked Thai vowels and tone marks never collide. ## Use it Load `kanok.css` after `tokens.css` (see [Installation](https://parntengza.github.io/kanok-ui-docs/en/installation/)), then mark where Kanok applies: ```html ``` `data-design` works on any element, not only ``. An app that runs inside another product's frame (a portal with its own header and side bar) puts it on its own root element, so the host page is left alone: ```html
…
``` ### React + Tailwind The React + Tailwind components read the same settings, so they follow Kanok too. Copy `kanok.css` next to the tokens (`npx shadcn@4 add @kanok-ui/kanok`) and import it after them: ```css @import 'tailwindcss'; @import './components/ui/primitives/tokens.css'; @import './components/ui/primitives/kanok.css'; @import './components/ui/react-tw/theme.css'; ``` Then set `data-design` as above. In a micro app (React in a shadow root) put `data-design` and `data-theme` on the host element. ## Outlined fields Kanok's fields are filled by default: a soft grey fill, no edge. For fields with an edge, say a long form with many fields close together, or a form on a grey surface (a tile, a layer) where the soft fill is hard to see, use the outlined look. To use it: 1. Turn Kanok on as in [Use it](#use-it) (`kanok.css` and `data-design="kanok"`). 2. Put `data-field-variant="outlined"` on an element around the fields: a `
`, a `
`, or `` for the whole app. Every field inside follows; there's nothing to set per field and no component code to change. 3. It works the same for the web components, the React wrappers and React + Tailwind, since it's a plain HTML attribute. Below, filled (left) and outlined (right) side by side: HTML: ```html
``` React: ```tsx
``` React + Tailwind: ```tsx
``` The whole app outlined: ```html ``` | State | filled (default) | outlined | | --- | --- | --- | | Rest | soft grey fill, no edge | 1px edge, no fill | | Focus | 4px blue halo | the same edge + 4px blue halo | | Invalid | red halo | the same edge + red halo | | Disabled | soft grey fill, faint text | faint edge, faint text | The edge follows the theme (`paper` / `lacquer`) and is set by `--ui-field-outline-color`; a theme zone nested inside stays outlined. Keep one look per form. ## Light and dark Kanok has two themes: `paper` (light) and `lacquer` (dark). `data-theme="auto"` follows the system setting. You don't need to change anything for `` or `useThemeMode`: on a Kanok page their light / dark choice becomes paper / lacquer. The core functions are in `@purpurabog/ui/core`: | Function | Does | | --- | --- | | `applyDesign('kanok')` | sets `data-design` on `` and moves the theme to the same mode in Kanok | | `designScript()` | a `` script that paints the remembered style (or `?design=` from the URL) on the first frame; put it before `themeScript()` | | `themeIn('kanok', 'g100')` | the theme a design shows for a mode: `lacquer` | ## Adjust it Kanok is a set of values for public CSS custom properties: colours (`--ui-primary`, `--ui-border-interactive`, …) and shape settings (`--ui-button-radius`, `--ui-field-border`, `--ui-card-radius`, `--ui-menu-item-radius`, …). Override any of them on a zone, as in [Customizing](https://parntengza.github.io/kanok-ui-docs/en/customizing/). Each element's settings are listed in its API reference. ## Status Kanok is new. These components have their Kanok shape: button, the form fields (text input, text area, number, select, search, dropdown, combobox, multi-select, date and time pickers), checkbox, radio, toggle, tags, tabs, tiles, cards, tables (with a mark on a chosen row), menus, notifications, modals, button sets, code snippets, contained tabs (as a segmented control), the calendar, the header, the side nav, the content switcher, pagination, the slider, the file uploader, tooltips, popovers, toggletips, overflow menus, side panels and tearsheets. Every other component already takes Kanok's colours, font and sizes, and gets its shape in the next releases. The React + Tailwind components follow Kanok as well. --- # Customize the look Start from the shallowest layer and go deeper only when it isn't enough: 1. **Design tokens** (`--ui-*`) for sizes and colors 2. **Theme zones** (`data-theme`, see [Themes and layers](https://parntengza.github.io/kanok-ui-docs/en/themes/)) 3. **`::part()`** for one piece of an element 4. **Slots** for markup in labels and help text 5. **A derived class** as the last resort The shallower layers survive upgrades better, because they don't depend on how an element is built inside. Attributes, properties, events, slots, parts and `--ui-*` tokens listed in [api.md](https://cdn.jsdelivr.net/npm/@purpurabog/ui@latest/docs/api.md) are not removed or renamed before the next major version. Selectors inside the shadow DOM are not part of that promise. ## Change sizes and colors with tokens Every component reads CSS custom properties named `--ui-*` from `tokens.css` (loaded once per page). Set them on `:root` to change the whole app, or on any element to change only what's inside it. You don't touch the shadow DOM: HTML: ```html
``` React: ```tsx // .compact is the same CSS rule as in the HTML tab
``` React + Tailwind: ```tsx const numbers = [ { value: '1', label: 'One' }, { value: '2', label: 'Two' }, { value: '3', label: 'Three' }, { value: '4', label: 'Four' }, ];
``` The React + Tailwind classes read the same tokens (see below), so a token set on a wrapper changes them too. Headless parts have no look, so tokens don't apply to them. The public tokens that components read: | Token | Changes | |---|---| | `--ui-field-height-sm`, `-md`, `-lg` | Field height per `size` (32 / 40 / 48px): text input, select, dropdown, combobox, multi-select, date picker, button | | `--ui-menu-max-rows` | Rows a dropdown, combobox or multi-select menu shows before it scrolls (5.5) | | `--ui-calendar-width` | Width of the date picker's calendar (288px) | | `--ui-sheet-radius` | Top corners of the date picker's bottom sheet on phones (the menu radius) | | `--ui-available-height`, `--ui-available-width` | Written on an open list or menu while it is open: the room left beside its field. Its max-height takes it, so a long list scrolls inside the screen. Read it in your own popup's CSS; don't set it | | `--ui-highlight` | Color of the selected days in a range picker | | `--ui-surface-1`, `--ui-surface-2`, `--ui-hairline`, `--ui-ink-subtle`, `--ui-ghost-hover`, `--ui-ghost-active` | Layer tokens: they point at the current layer's set, and `` moves them | | `--ui-primary`, `--ui-ink`, `--ui-canvas`, `--ui-focus`, … | Theme colors, each made from the Carbon token named at the end of its line in `tokens.css` | Each element's own list is under "Design tokens" in [api.md](https://cdn.jsdelivr.net/npm/@purpurabog/ui@latest/docs/api.md) and in the CSS custom properties table of its Storybook page. ## Change one piece of an element with `::part()` `::part(name)` styles a piece an element exposes. Part names are the same words across components: `base`, `label`, `input`, `menu`, `option`, `helper-text`, `error-text`, … The date picker passes on the parts of the fields inside it; in range mode they get `start-` and `end-` in front: HTML: ```html ``` React: ```tsx // the same CSS as in the HTML tab: the wrappers render and ``` Every element's parts are listed in [api.md](https://cdn.jsdelivr.net/npm/@purpurabog/ui@latest/docs/api.md). `::part()` is for the web components and the React wrappers, which render them. React + Tailwind and headless have no shadow DOM: give the pieces classes instead (the last two sections). A few elements have no pieces inside: `ui-toaster`, `ui-fluid-form`, `ui-button-set`, `ui-badge-indicator` and `ui-stack` are the box you see. Style them with plain CSS on the element, e.g. `ui-stack { row-gap: 24px; }` or `ui-badge-indicator { background: var(--ui-info); }`: page CSS wins over their own. `ui-button-set` also has the part `button-set` (as in Carbon), but its layout is on the element. ## Style by state You can read every state that changes the look from the element itself, and combine it with `::part()`: - **Properties you or the user change** are reflected as attributes: `open`, `checked`, `selected`, `expanded`, `invalid`, `warn`, `size`, `kind`, … - **States the element works out itself** are set as `data-*` attributes on the element: | Attribute | Means | On | |---|---|---| | `data-open` | The list or calendar is open | `ui-dropdown`, `ui-combobox`, `ui-multi-select`, `ui-date-picker` | | `data-show-error`, `data-show-warn` | The error or warning is actually showing (an error replaces a warning) | every form field | | `data-has-value` | There is text or a selection | `ui-combobox`, `ui-multi-select`, `ui-search` | | `data-placeholder` | The placeholder is showing | `ui-dropdown`, `ui-select` | | `data-password-visible` | The password is shown | `ui-text-input type="password"` | | `data-editing` | In edit mode | `ui-edit-in-place` | | `data-drag-over` | A file is dragged over the drop zone | `ui-file-uploader` | | `data-shown` | The spinner or overlay really shows (after `delay`) | `ui-loading` | | `data-overflowing` | The text is cut off | `ui-truncated-text` | Each of these is also a custom state, as on native and Web Awesome elements: `ui-dropdown:state(open)` matches exactly when `ui-dropdown[data-open]` does (`:state(show-error)`, `:state(has-value)`, …). Use whichever you prefer; the attribute works in older browsers too. Each element's states are listed in its API reference. Click into the email field and leave it empty, then open the dropdown: HTML: ```html ``` React: ```tsx // the same CSS as in the HTML tab: the states are set on the elements the wrappers render ``` Use `[data-show-error]` rather than `[invalid]` to style the error the user actually sees. `invalid` is the value you set: it stays on a disabled field, which shows no error, and it doesn't include errors from the user's own input. The `data-*` states are set after the first render in the browser, so HTML rendered on a server doesn't have them yet. The headless parts show their state with `data-*` attributes of their own: see [Headless](https://parntengza.github.io/kanok-ui-docs/en/headless/#style-on-data-attributes). ## Put markup in labels and help text with slots When text needs a link, bold text or an icon, use a slot instead of the attribute of the same name. The attribute still works as plain-text fallback. HTML: ```html Email (required) Use your work email. See the policy. ``` React: ```tsx Email (required) Use your work email. See the policy. ``` React + Tailwind: ```tsx const countries = [ { value: 'th', label: 'Thailand', description: 'UTC+7' }, { value: 'jp', label: 'Japan', description: 'UTC+9' }, ]; Email (required)} helperText={<>Use your work email. See the policy.} /> ``` Every field takes the `label`, `helper-text`, `warn-text` and `invalid-text` slots. `data-description` on an `