# LinkNode

<!-- schema:start — tự sinh từ link.schema.ts, không sửa tay -->

Liên kết (thẻ a). Chứa text, hoặc các node con fragment, inline. Không lồng link trong link.

## Resolved name

`link`

## Configurable props

### Props

| Prop | Giá trị | Responsive | Mô tả |
| --- | --- | --- | --- |
| `href` | `https://… · /… · #… · mailto:… · tel:…` | không | Đích của liên kết |
| `target` | `'_self' · '_blank'` | không | Cách mở liên kết |
| `rel` | `chuỗi` | không | Thuộc tính rel, ví dụ nofollow, noreferrer, noopener, ugc |

### `sx`

| Key | Giá trị | Breakpoint |
| --- | --- | --- |
| `color` | `color` | chỉ mobile (xs) |
| `fontWeight` | `200 · 300 · 400 · 500 · 600 · 700 · 800` | mọi breakpoint |
| `fontStyle` | `'italic'` | chỉ mobile (xs) |
| `textDecorationLine` | `'underline'` | chỉ mobile (xs) |
| `textTransform` | `'uppercase' · 'capitalize' · 'lowercase'` | chỉ mobile (xs) |
| `textAlign` | `'left' · 'center' · 'right' · 'justify'` | mọi breakpoint |
| `marginTop` | `px · 'auto'` | mọi breakpoint |
| `marginRight` | `px · 'auto'` | mọi breakpoint |
| `marginBottom` | `px · 'auto'` | mọi breakpoint |
| `marginLeft` | `px · 'auto'` | mọi breakpoint |
| `paddingTop` | `px` | mọi breakpoint |
| `paddingRight` | `px` | mọi breakpoint |
| `paddingBottom` | `px` | mọi breakpoint |
| `paddingLeft` | `px` | mọi breakpoint |
| `borderStyle` | `'solid' · 'dashed' · 'dotted'` | mọi breakpoint |
| `borderTopWidth` | `px` | mọi breakpoint |
| `borderRightWidth` | `px` | mọi breakpoint |
| `borderBottomWidth` | `px` | mọi breakpoint |
| `borderLeftWidth` | `px` | mọi breakpoint |
| `borderColor` | `color` | mọi breakpoint |
| `borderTopLeftRadius` | `px 0..9999` | mọi breakpoint |
| `borderTopRightRadius` | `px 0..9999` | mọi breakpoint |
| `borderBottomRightRadius` | `px 0..9999` | mọi breakpoint |
| `borderBottomLeftRadius` | `px 0..9999` | mọi breakpoint |
| `fontSize` | `số ≥ 0 · 'var(--…)' · 'N' · 'Npx' · 'Nrem' · 'Nem' · 'N%'` | mọi breakpoint |
| `lineHeight` | `số ≥ 0 · 'var(--…)' · 'N' · 'Npx' · 'Nrem' · 'Nem' · 'N%'` | mọi breakpoint |

`color` = `var(--color-{primary\|secondary\|red\|orange\|yellow\|green\|blue\|violet\|neutral}-{100..800\|contrast}) · var(--color-background-{subtle\|default\|emphasis}) · var(--color-border-default) · var(--color-surface-default) · var(--color-common-{black\|white}) · #rgb · #rrggbb`

## Rules

- **Nội dung**: chỉ nhận `fragment`, `inline`, hoặc văn bản thay cho node con.
- **Cha hợp lệ**: bất kỳ.
- sx.fontSize và sx.lineHeight phải đi cùng nhau và là một cặp có trong typographyPresets của consumer.

<!-- schema:end -->

## Behavior

- Render qua component Link do consumer cấp (`resources.nodes.link.component`), mặc định là thẻ `<a>`.
- Trong editor, click bị `preventDefault` để tránh điều hướng khỏi canvas.
- Khi prop `children` đang là text inline thì không kéo thêm node con vào được.

## Usage

```tsx
import { LinkNode } from '@janbox/storefront-builder';

// Link đơn giản
<LinkNode href="/products">View all products</LinkNode>

// Mở tab mới, no-follow
<LinkNode href="https://example.com" target="_blank" rel="nofollow noreferrer">
  External link
</LinkNode>

// Link có viền bo tròn, padding
<LinkNode
  href="/checkout"
  sx={{
    paddingTop: 12,
    paddingBottom: 12,
    paddingLeft: 24,
    paddingRight: 24,
    borderStyle: 'solid',
    borderTopWidth: 1,
    borderRightWidth: 1,
    borderBottomWidth: 1,
    borderLeftWidth: 1,
    borderColor: '#fa8c16',
    borderTopLeftRadius: 6,
    borderTopRightRadius: 6,
    borderBottomLeftRadius: 6,
    borderBottomRightRadius: 6,
    color: '#fa8c16',
  }}
>
  Checkout
</LinkNode>

// Cỡ chữ responsive
<LinkNode
  href="/sale"
  sx={{
    fontSize: 'var(--text-sm--font-size)',
    lineHeight: 'var(--text-sm--line-height)',
    md: {
      fontSize: 'var(--text-base--font-size)',
      lineHeight: 'var(--text-base--line-height)',
    },
  }}
>
  Sale items
</LinkNode>
```

## Link component

Consumer cấp component render link qua `resources.nodes.link.component` của `Composer` / `Editor`. Không cấp thì `LinkNode` render thẻ `<a>`.

```ts
type LinkComponentProps = ComponentPropsWithRef<'a'>; // props native của <a>, href optional
type LinkComponent = ComponentType<LinkComponentProps>;
```

Yêu cầu với component được inject:

- Forward `ref` tới thẻ `<a>`. Nếu không, node không chọn hay kéo được trong editor.
- Truyền tiếp `className`, `onClick`, các thuộc tính `data-*` và `contentEditable` xuống thẻ `<a>`.

```tsx
import { Link } from 'react-router';
import NextLink from 'next/link';
import type { LinkComponentProps } from '@janbox/storefront-builder';

// React Router
const AppLink = ({ href, ...props }: LinkComponentProps) =>
  href ? <Link {...props} to={href} /> : <a {...props} />;

// Next.js
const AppLink = ({ href, ...props }: LinkComponentProps) =>
  href ? <NextLink {...props} href={href} /> : <a {...props} />;

<Composer resources={{ nodes: { link: { component: AppLink } } }}>…</Composer>
```

> `next/link` prefetch các link thấy được trên màn hình, kể cả trong canvas editor. Muốn tắt thì truyền `prefetch={false}` trong adapter.

## Nâng cấp từ 4.x

| 4.x | 5.0.0 |
| --- | --- |
| `to` | `href` |
| `textVariant: 'X'` | `sx.fontSize: 'var(--text-X--font-size)'` + `sx.lineHeight: 'var(--text-X--line-height)'` |
| `underline: true` | `sx.textDecorationLine: 'underline'` |
| `sm`/`md`/`lg` cấp trên cùng | `sx.sm`/`sx.md`/`sx.lg` |
| (không có) | Inject `nodeResources.link.component` (từ 7.0.0: `resources.nodes.link.component`), mặc định `<a>` |

Thứ tự: thêm key mới trước, chỉ xoá `to` / `textVariant` / `underline` sau khi mọi nơi render đã chạy 5.0.0.

## Related nodes

- [`ButtonNode`](../button/README.md) — dùng khi cần CTA có style button từ theme
