# Sheet / sidebar

Sidebar của AI Mode: cột phải trên desktop, slide-over trên mobile, giữ focus và trả focus về nơi mở.

## Key rules

- Dùng `Sidebar` qua `SidebarHost`: mỗi lúc chỉ một sidebar, nội dung đăng ký trong `SIDEBAR_REGISTRY`.
- Desktop là cột phải rộng 430px, không có nền chắn; mobile (≤899px) là slide-over phủ cả widget và có scrim.
- Header cao 52px gồm tiêu đề và nút đóng; panel mobile dùng `--radius-panel`.
- Esc đóng, Tab bị giữ trong sidebar, focus vào nút đóng khi mở và trả về nơi mở khi đóng xong.
- Element `chatty-sheet` ở mục Standard là chuẩn chatty-ui port theo; mục AI Mode mô tả component React đang chạy.

## Use when / Avoid

Dùng cho chi tiết sản phẩm, đơn hàng hoặc giỏ hàng cần vùng đọc riêng mà vẫn giữ hội thoại. Classic và Admin chưa có sidebar dùng chung trong code. Danh sách hành động ngắn dùng menu; tray hành động theo lựa chọn ở đáy panel mobile dùng SelectionSheet.

### Use instead

| Component | Use when |
| --- | --- |
| `Sidebar` | Chi tiết sản phẩm, đơn hàng hoặc giỏ hàng cần cột riêng; chỉ AI Mode. |
| `SelectionSheet` | Tray hành động cho sản phẩm đã chọn, cố định ở đáy panel và chỉ mobile. |
| `ComposerMenu` | Danh sách hành động ngắn quanh composer; xem trang Menu / popover. |
| `Modal` | Dialog overlay cấp thấp của kit có focus trap và Esc; không phải sidebar. |

## Live preview

Example (Standard) — Element chatty-sheet layout column: cột phải cạnh hội thoại.

```html
<chatty-sheet open style="height: 360px; margin-inline-start: auto">
  <span slot="heading">Classic leather boot</span>
  <p style="margin: 0">Full-grain leather, a cushioned insole and a rubber sole. Free returns within 30 days.</p>
</chatty-sheet>
```

Example (Standard) — Layout overlay: trượt lên phủ widget, có scrim và tay nắm.

```html
<chatty-sheet open layout="overlay" style="position: relative; width: 340px; height: 420px">
  <span slot="heading">Your bag</span>
  <p style="margin: 0">2 items · $148.00</p>
</chatty-sheet>
```

Example (Current (React), being replaced) — Sidebar thật mở với tiêu đề, nút đóng và thân nội dung: nút Desktop của preview hiện cột phải, nút Mobile hiện slide-over có handle, nút giỏ hàng và scrim.

```jsx
import React, {useRef} from 'react';
import {Sidebar} from '@scripttag/v2/components/Sidebar/Sidebar.jsx';
import {useIsMobileViewport} from '@scripttag/v2/hooks/useIsMobileViewport.js';
// Defines --aimode-sidebar-open-width (430px) on .chatty-ai-mode; the widget loads it with the panel.
import '@scripttag/v2/components/Panel.scss';

const Title = () => 'Order #1042';
const noop = () => {};

function Body() {
  return <p style={{margin: 12}}>Body content</p>;
}

// The frame follows the page's Desktop / Mobile switch: wider than 899px it is the desktop right column, otherwise the real slide-over. The frame root already carries .chatty-ai-mode and .is-mobile, so the specimen only supplies the positioned box the widget root would be.
export function Catalogue() {
  const ref = useRef(null);
  const isMobile = useIsMobileViewport(ref);
  return (
    <div
      ref={ref}
      style={{
        position: 'relative',
        height: isMobile ? 480 : 360,
        overflow: 'hidden',
        display: 'flex',
        justifyContent: 'flex-end'
      }}
    >
      <Sidebar open showBag={isMobile} title={Title} onClose={noop} onExited={noop}>
        <Body />
      </Sidebar>
    </div>
  );
}
```

## Standard

Example (Standard) — Element chatty-sheet layout column: cột phải cạnh hội thoại.

```html
<chatty-sheet open style="height: 360px; margin-inline-start: auto">
  <span slot="heading">Classic leather boot</span>
  <p style="margin: 0">Full-grain leather, a cushioned insole and a rubber sole. Free returns within 30 days.</p>
</chatty-sheet>
```

Prototype  (cùng `sheet.styles.js` và , dùng `FocusController`) là chuẩn: chatty-ui port 1:1 các file này, và `chatty-ui/custom-elements.json` sinh từ chính prototype. Sheet không lên top layer: overlay chỉ phủ tổ tiên có position (widget), không phủ cả trang của merchant. Layout column không modal (role complementary): Tab đi ra composer và thread được. Chỉ overlay là modal (role dialog, aria-modal) và giữ Tab bên trong. Hai layout đều chuyển focus tới nút đóng khi mở và đóng bằng Esc. Chưa có kéo để đóng nên element không vẽ tay nắm. Chưa surface nào dùng element thật; mục AI Mode bên dưới mô tả React đang chạy.

### Element

| Element | Module |
| --- | --- |
| `chatty-sheet` | @avada/chatty-ui/elements/sheet.js |

### API

| Kind | Name | Type | Default | Description |
| --- | --- | --- | --- | --- |
| attribute | `open` | boolean | `false` | Sheet đang mở; mở sau khi gắn vào trang thì chuyển focus tới nút đóng. Sheet mở sẵn lúc gắn vẫn có Esc và trap nhưng không lấy focus. |
| attribute | `layout` | column, overlay | `'column'` | column là cột phải cạnh hội thoại, không modal; overlay trượt lên phủ tổ tiên có position, kèm scrim, modal và giữ Tab. |
| attribute | `label` | string | `''` | Tên truy cập; để trống thì lấy từ slot heading. |
| attribute | `close-label` | string | `'Close'` | Tên truy cập của nút đóng. |
| event | `click` | MouseEvent | – | Click native của nút đóng, nổi ra ngoài shadow root. |
| event | `chatty-close` | CustomEvent<{reason: escape, backdrop, close-button}> | – | Yêu cầu đóng (escape, backdrop hoặc close-button); preventDefault giữ sheet mở. |
| slot | default | – | – | Thân sheet. |
| slot | `heading` | – | – | Tiêu đề; đặt tên cho sheet khi label trống. |
| slot | `actions` | – | – | Control ở header, đứng trước nút đóng. |
| slot | `close-icon` | – | – | Icon của nút đóng; mặc định là dấu nhân. |
| part | `base` | – | – | Khung sheet (role complementary; dialog ở layout overlay). |
| part | `scrim` | – | – | Nền chắn của layout overlay. |
| part | `header` | – | – | Hàng tiêu đề. |
| part | `close` | – | – | Nút đóng. |
| part | `body` | – | – | Vùng chứa thân. |
| css-property | `--chatty-sheet-width` | – | – | Chiều rộng cột ở layout column. |

## AI Mode

### Import / element

| Component / element | Import / file | Scope root |
| --- | --- | --- |
| `Sidebar` | @scripttag/v2/components/Sidebar/Sidebar.jsx | .chatty-ai-mode[data-theme] .chatty-sidebar |
| `SidebarHost` | @scripttag/v2/components/Sidebar/SidebarHost.jsx | .chatty-ai-mode[data-theme] .chatty-sidebar |

### Anatomy

| Part | Element |
| --- | --- |
| 1 | Scrim `chatty-sidebar-scrim`, `aria-hidden="true"`, chỉ hiện trên mobile. |
| 2 | Container `chatty-sidebar` với `role="dialog"` và `aria-modal="true"`. |
| 3 | Handle kéo `chatty-sidebar-handle`, chỉ hiện trên mobile. |
| 4 | Header: tiêu đề `chatty-sidebar-title` và nhóm `chatty-sidebar-actions`. |
| 5 | Nút đóng với `aria-label` Close; nút giỏ hàng chỉ khi `showBag`. |
| 6 | Thân `chatty-sidebar-body` chứa `children`, caller tự cuộn trong view của mình. |

### Placement / order

| Context | Order |
| --- | --- |
| Desktop | Cột phải trong lưới panel, không có nền chắn; panel mở rộng thêm 430px khi `has-sidebar`. |
| Mobile | Slide-over gắn ở root widget, phủ cả widget kể cả vùng composer; kích thước widget không đổi. |
| Trong sidebar | Handle, header (tiêu đề rồi nhóm nút giỏ hàng và đóng), thân. |

### Props

| Prop | Type | Values | Default | Notes |
| --- | --- | --- | --- | --- |
| `open` | boolean | true / false | Caller | Điều khiển class mở, `inert` và focus. |
| `title` | component | Component không props | Caller | Render trong header. |
| `titlePhase` | string | pha swap của view | rest | Chọn class `chatty-view-` cho tiêu đề. |
| `showBag` | boolean | true / false | Caller | Icon giỏ hàng trong header, chỉ hiển thị trên mobile. |
| `onClose` | Function | Handler | Caller | Gọi từ nút đóng, scrim, Esc và kéo handle. |
| `onExited` | Function | Handler | Caller | Gọi một lần sau khi animation thoát kết thúc. |
| `children` | ReactNode | Nội dung view | Caller | Render trong thân sidebar. |

### Sizes

| Size | Height | Padding | Gap | Radius | Font role (px/rem) | Hit target | Breakpoint / mechanism |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Cột desktop | 100% của panel | Không khai báo | Không khai báo | Không khai báo | Không có chữ | Theo nội dung | width 430px qua `--aimode-sidebar-open-width`; border-left 1px |
| Header | 52px | 0 theo `--chatty-layout-inset` | Không khai báo | Không khai báo | Theo tiêu đề | Theo nút | border-bottom 1px |
| Tiêu đề | Theo line-height của role | Không khai báo | 6px | Không khai báo | `heading-large`: Derived(17px = type(heading-large, desktop, size)) / Derived(24px = type(heading-large, desktop, line)) desktop, Derived(19px = type(heading-large, mobile, size)) / Derived(26px = type(heading-large, mobile, line)) mobile; weight 600 | Không tương tác | JS .is-mobile khi width ≤899px; hoặc media (hover: none) and (pointer: coarse), kể cả landscape >899px |
| Nhóm nút | Theo nút | Không khai báo | 4px | Không khai báo | Không có chữ | Theo nút | Không override theo viewport |
| Slide-over mobile | inset 0, phủ cả widget | Không khai báo | Không khai báo | 28px (`--radius-panel`) | Không có chữ | Cả widget | `.is-mobile` khi width ≤899px |
| Handle mobile | 4px (thanh) | 6px 0 14px | Không khai báo | 2px | Không có chữ | Rộng min(140px, 36%) | Chỉ `.is-mobile`; kéo xuống để đóng |

### Tokens

| Part | Token | Light | Dark |
| --- | --- | --- | --- |
| Cạnh cột và header | `--glass-edge` | rgba(20,19,24,0.08) | rgba(255,255,255,0.12) |
| Tiêu đề | `--vib-1` | rgba(20,19,24,0.94) | rgba(255,255,255,0.96) |
| Thanh handle | `--vib-3` | rgba(20,19,24,0.62) | rgba(255,255,255,0.62) |
| Handle khi kéo | `--accent` | Merchant --faqsBtnPopupColor, fallback #8b7be8 | Same definition |
| Nền slide-over mobile | `--glass-opaque-regular` | rgb(250,249,246) | rgb(24,23,28) |
| Scrim mobile | `rgb(0 0 0 / 0.4)` | rgb(0 0 0 / 0.4) | rgb(0 0 0 / 0.4) |
| Mở slide-over | `--chatty-motion-sheet-in` | Derived(400ms = duration(400)) | Same |
| Đóng slide-over | `--chatty-motion-sheet-out` | Derived(250ms = duration(250)) | Same |

### States

| State | Trigger | Visual change (values) | ARIA/attribute |
| --- | --- | --- | --- |
| Closed | `open` false | Bị `inert`; mobile: translateY(100%), scrim opacity 0. | `inert` |
| Open | `open` true, sau hai frame | Class `is-open`; mobile: translateY(0), thời gian `--chatty-motion-sheet-in`, scrim opacity 1. | `role="dialog"`, `aria-modal="true"` |
| Dragging | Kéo handle trên mobile | Handle rộng 48px, nền `--accent`. | `aria-hidden="true"` trên handle |
| Reduced motion | `prefers-reduced-motion: reduce` | Chỉ chuyển opacity trong 150ms; transform none; chưa mở opacity 0, mở opacity 1. | Không thêm ARIA |

### Behaviour

| Interaction | Result |
| --- | --- |
| Mở | Ghi nhớ phần tử đang focus làm opener rồi focus nút đóng ở frame kế tiếp. |
| Tab | Giữ focus trong sidebar: Tab ở phần tử cuối quay về đầu, Shift+Tab ở đầu quay về cuối. |
| Esc | Gọi `onClose`. |
| Scrim và kéo handle | Bấm scrim gọi `onClose`; kéo handle xuống gọi `onClose` qua `useDragToDismiss`; cả hai chỉ có trên mobile. |
| Đóng xong | Chờ `transitionend` của transform hoặc opacity, rồi trả focus về opener và gọi `onExited` đúng một lần. |
| Chọn view | `SidebarHost` render một sidebar đang hoạt động theo `SIDEBAR_REGISTRY` (bag, product-detail, order-detail) và chuyển view bằng fade-through. |
| Nút giỏ hàng | `showBag` bật khi view không phải giỏ hàng; nút chỉ hiển thị ở mobile. |

### Copy

| String | i18n key | Where |
| --- | --- | --- |
| Close | Close | `aria-label` của nút đóng |
| Shopping bag | Shopping bag | Nhãn nút giỏ hàng trong header |
| Your cart | Your cart | Tiêu đề view giỏ hàng |

### Source

| File | Evidence |
| --- | --- |
|  | Cấu trúc, focus, Esc và thoát |
|  | Kích thước, mobile và motion |
|  | Host và registry |

## Do / Don't

Do: giữ tiêu đề, nút đóng và thân nội dung cuộn ổn định giữa các view.

Don't: không mở hai sidebar cùng lúc hoặc đặt nút đóng ngoài header.

## Accessibility

| Surface | Role / name | Keyboard / focus | Live region | Contrast pair / ratio |
| --- | --- | --- | --- | --- |
| AI Mode | Container `role="dialog"` và `aria-modal="true"`; chưa gán tên từ tiêu đề; nút đóng tên "Close". | Focus vào nút đóng khi mở; Tab bị giữ trong sidebar; Esc đóng; focus trả về opener sau khi thoát. | Không có live region trong Sidebar | Mobile, tiêu đề `--vib-1` trên `--glass-opaque-regular`: Light Measured(rgba(20,19,24,0.94) / rgb(250,249,246) = 15.23:1); Dark Measured(rgba(255,255,255,0.96) / rgb(24,23,28) = 16.46:1) |

## Target

```target
| Surface | Now | Target | Source |
| --- | --- | --- | --- |
| AI Mode | Container có `role="dialog"` nhưng chưa có tên truy cập từ tiêu đề. | Not in code yet — build with the body values. Gắn `aria-labelledby` từ container tới phần tử tiêu đề để dialog có tên. | [WAI-ARIA APG — dialog modal](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/) |
| AI Mode | Sidebar React, slide-over trên mobile | Not available — new build follows Target. Port `chatty-sheet` từ mục Standard: layout column trên desktop (không modal), overlay trên mobile (modal, trap); Esc và restore focus qua `FocusController`. Kéo để đóng chưa có nên element không vẽ tay nắm. |  |
```


- [WAI-ARIA APG — dialog modal](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/)
