# 链接

## 使用方法

`Link` 用于在应用内部或外部之间建立导航。有 `href` 时默认渲染为 `<a>`，也可以用 `as` 换成路由链接或其他组件，组件会同时收到 `href` 和 `to`。没有 `href` 时降级为 `<button>`。禁用后仍是普通锚点，不再使用自定义根。`size` 和 `weight` 跟正文同一套字号和字重，`maxLines` 限制可见行数。从独立入口导入后，结构样式会随组件自动加载：

```vue
<script setup lang="ts">
import { Link } from '@astryx-vue/core/Link'
</script>

<template>
  <Link href="/docs">
    文档
  </Link>
  <Link href="https://github.com" is-external-link>
    GitHub
  </Link>
</template>
```

### 结构

- **标签**: 链接的可见文字。
- **右侧图标**: 放在标签后面，表示这个链接会做什么。
- **左侧图标**: 放在标签前面，表示含义。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 链接文字应直接描述目标，例如“查看文档”“联系我们”，避免使用“点击这里”这类含糊表达。 |
| 推荐 | 跳转到站外的链接使用 `isExternalLink`，组件会自动添加 `target="_blank"` 和 `rel="noopener noreferrer"`，并给出外部标识。 |
| 推荐 | 在一组链接或长段落中，使用 `color="secondary"` 降低视觉噪音，让真正的重点操作更突出。 |
| 推荐 | 想让链接跟着周围文字一起截断，就把它放进设了 `maxLines` 的 `Text` 里；这时链接会并入文字的行里。 |
| 避免 | 不要指望外面的截断能切开自己设了 `maxLines` 的链接，或者没有 `href`、渲染成按钮的链接；它们保留自己的盒子。 |
| 避免 | 避免把页面上的大量文字都做成链接：过多的下划线或强调色会削弱链接本身的可发现性。 |
| 避免 | 避免把关键操作只做成链接。保存、删除、提交等核心操作应使用 `Button`，让用户明确感知这是一个动作。 |

## 示例

### 外部链接

纵向排列的外部链接，在新标签页打开，并带有外部指示图标。

```vue
<script setup lang="ts">
import { Link } from '@astryx-vue/core/Link'
import { VStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <VStack :gap="2">
    <Link href="https://github.com" is-external-link is-standalone>
      GitHub
    </Link>
    <Link href="https://developer.mozilla.org" is-external-link is-standalone>
      MDN Web Docs
    </Link>
    <Link href="https://react.dev" is-external-link has-underline is-standalone>
      React Documentation
    </Link>
  </VStack>
</template>
```

### 行内文字

嵌在正文段落里的链接。

```vue
<script setup lang="ts">
import { Link } from '@astryx-vue/core/Link'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Text type="body">
    Read the <Link href="#">
      documentation
    </Link> for more information about using Astryx components.
  </Text>
</template>
```

### 带提示

一排独立链接，悬停时用提示说明去向。

```vue
<script setup lang="ts">
import { Link } from '@astryx-vue/core/Link'
import { HStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <HStack align="center" :gap="4">
    <Link href="#" tooltip="Configure your account settings" is-standalone>
      Settings
    </Link>
    <Link href="#" tooltip="View and edit your profile" is-standalone>
      Profile
    </Link>
    <Link href="#" tooltip="Get help and support" color="secondary" is-standalone>
      Help
    </Link>
  </HStack>
</template>
```

### 限制行数

窄容器里最多显示两行，字号偏小，字重偏重。

```vue
<script setup lang="ts">
import { Link } from '@astryx-vue/core/Link'
</script>

<template>
  <div class="link-preview" style="width: 16rem">
    <Link href="#notes" :max-lines="2" size="sm" weight="semibold">
      Field notes from the harbor survey, including the tide table and the list of boats still in the channel.
    </Link>
  </div>
</template>
```

## Props

| 名称 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `href` | `string` | - | 链接目标地址。未提供时渲染为 `<button>`。 |
| `as` | `LinkRoot` | - | 有地址且未禁用时替换 `<a>` 的根组件，同时接收 `href` 和 `to`。 |
| `label` | `string` | - | 无障碍名称。可见内容不是名称时使用，例如只有图标的链接。设置后会代替链接文字被朗读。 |
| `newTabLabel` | `string` | 当前语言的「（在新标签页打开）」 | 外部链接后面给读屏的说明。 |
| `size` | `LinkSize` | - | 字号，和正文相同：sm、md、lg、xl。 |
| `weight` | `LinkWeight` | - | 字重。normal 和 regular 相同。 |
| `maxLines` | `number` | `0` | 最多显示的行数。0 表示不截断。 |
| `isExternalLink` | `boolean` | `false` | 是否在新标签页打开并显示外部链接标识。 |
| `hasUnderline` | `boolean` | `false` | 是否始终显示下划线。 |
| `color` | `LinkColor` | `'accent'` | 链接颜色，可选 primary、secondary、accent、inherit。 |
| `isDisabled` | `boolean` | `false` | 是否禁用。禁用后不能跳转，也不触发 `click`；它保留 Tab 焦点位置，读屏会读出「已禁用」。调用方传的 `role` 或 `aria-disabled` 不会替换掉这一点，调用方传的 `tabindex` 照常生效。 |
| `target` | `string` | - | HTML target 属性。 |
| `rel` | `string` | - | HTML rel 属性。外部链接会自动追加 noopener noreferrer。 |
| `download` | `string \| boolean` | - | 是否触发下载。写字符串就是建议的文件名。`true` 只写一个空的 `download` 属性，`false` 不写这个属性。 |
| `referrerPolicy` | `string` | - | 链接的 referrer 策略，原样转成 `referrerpolicy` 属性。 |

## 事件

| 名称 | 说明 |
| --- | --- |
| `click` | 点击时触发。禁用时不会触发。 |

## 插槽

| 名称 | 说明 |
| --- | --- |
| `default` | 链接文字内容。 |

## 原生属性

根元素为 `<a>`、`<button>`，或 `as` 传入的组件。禁用的链接始终是没有地址的 `<a>`。

以下内容传入根元素：

- HTML 属性。
- `aria-*` 属性。
- `data-*` 属性。`data-color` 始终反映 `color`，调用方传的同名值不会替换它。
- `class`。
- `style`。

## 无障碍

### 链接文字

- 准则: 1.4.3 Contrast (Minimum)
- 要求: 4.5:1
- 适用状态: Rest, Hover, Pointer down

链接文字和后面的背景至少 4.5:1。按下时，对着链接画在文字后面的按下叠层来量。
