Skip to content

Commit

Permalink
feat: add breadcrumb and breadcrumbItem components (#123)
Browse files Browse the repository at this point in the history
* feat: add breadcrumb and breadcrumbItem components

* feat: add types, fix lint warn

* feat: add KSymbolKey fn, rewrite breadcrumb key
  • Loading branch information
vtrbo authored Aug 22, 2023
1 parent 6dfa245 commit 8f10021
Show file tree
Hide file tree
Showing 28 changed files with 526 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Test: KBreadcrumb > props: cls 1`] = `"<div class=\\"k-breadcrumb k-breadcrumb--test\\"></div>"`;
30 changes: 30 additions & 0 deletions components/Breadcrumb/__test__/breadcrumb.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { afterEach, expect, test, describe, beforeEach } from 'vitest';
import KBreadcrumb from '../src';

let host: HTMLElement;

const initHost = () => {
host = document.createElement('div');
host.setAttribute('id', 'host');
document.body.appendChild(host);
};
beforeEach(() => {
initHost();
});
afterEach(() => {
host.remove();
});

describe('Test: KBreadcrumb', () => {
test('props: cls', async () => {
const instance = new KBreadcrumb({
target: host,
props: {
cls: 'k-breadcrumb--test'
}
});
expect(instance).toBeTruthy();
expect((host as HTMLElement)!.innerHTML.includes('k-breadcrumb--test')).toBeTruthy();
expect(host.innerHTML).matchSnapshot();
});
});
39 changes: 39 additions & 0 deletions components/Breadcrumb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@ikun-ui/breadcrumb",
"version": "0.0.9-beta.2",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"svelte": "dist/index.js",
"types": "src/index.ts",
"keywords": [
"svelte",
"svelte3",
"web component",
"component",
"react",
"vue",
"svelte-kit",
"dx"
],
"scripts": {
"build": "npm run build:js && npm run build:svelte",
"build:js": "tsc -p . --outDir dist/ --rootDir src/",
"build:svelte": "svelte-strip strip src/ dist",
"publish:npm": "pnpm publish --no-git-checks --access public"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@ikun-ui/icon": "workspace:*",
"@ikun-ui/utils": "workspace:*",
"baiwusanyu-utils": "^1.0.14"
},
"devDependencies": {
"@tsconfig/svelte": "^5.0.0",
"svelte-strip": "^2.0.0",
"tslib": "^2.6.1",
"typescript": "^5.1.6"
}
}
22 changes: 22 additions & 0 deletions components/Breadcrumb/src/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script lang="ts">
import { setContext } from 'svelte';
import { getPrefixCls, createCls, BreadcrumbKey } from '@ikun-ui/utils';
export let separator: string = '/';
export let separatorIcon: string = '';
export let cls: string = '';
export let attrs: Record<string, string> = {};
setContext(BreadcrumbKey, {
separator,
separatorIcon
});
// class names
const prefixCls = getPrefixCls('breadcrumb');
$: cnames = createCls(prefixCls, cls);
</script>

<div class={cnames} {...$$restProps} {...attrs}>
<slot />
</div>
5 changes: 5 additions & 0 deletions components/Breadcrumb/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="./types" />
import Breadcrumb from './index.svelte';
export { Breadcrumb as KBreadcrumb };

export default Breadcrumb;
7 changes: 7 additions & 0 deletions components/Breadcrumb/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference types="svelte" />
export type KBreadcrumbProps = {
separator: string;
separatorIcon: string;
cls: string;
attrs: Record<string, string>;
};
11 changes: 11 additions & 0 deletions components/Breadcrumb/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@tsconfig/svelte/tsconfig.json",

"compilerOptions": {
"noImplicitAny": true,
"strict": true,
"declaration": true
},
"include": ["src/**/*.ts", "src/**/*.svelte"],
"exclude": ["node_modules/*", "**/*.spec.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Test: KBreadcrumbItem > props: cls 1`] = `"<span class=\\"k-breadcrumb-item k-breadcrumb-item--test\\"><span class=\\"k-breadcrumb-item-content k-breadcrumb-item-content__dark\\" href=\\"\\"></span> <span class=\\"k-breadcrumb-item-separator k-breadcrumb-item-separator__dark\\">/</span></span>"`;
exports[`Test: KBreadcrumbItem > props: href is empty 1`] = `"<span class=\\"k-breadcrumb-item\\"><span class=\\"k-breadcrumb-item-content k-breadcrumb-item-content__dark\\" href=\\"\\"></span> <span class=\\"k-breadcrumb-item-separator k-breadcrumb-item-separator__dark\\">/</span></span>"`;
exports[`Test: KBreadcrumbItem > props: href is not empty 1`] = `"<span class=\\"k-breadcrumb-item\\"><a class=\\"k-breadcrumb-item-content k-breadcrumb-item-content__dark k-breadcrumb-item-link\\" href=\\"https://github.com/ikun-svelte/ikun-ui\\"></a> <span class=\\"k-breadcrumb-item-separator k-breadcrumb-item-separator__dark\\">/</span></span>"`;
57 changes: 57 additions & 0 deletions components/BreadcrumbItem/__test__/breadcrumb-item.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { afterEach, expect, test, describe, beforeEach } from 'vitest';
import KBreadcrumbItem from '../src';

let host: HTMLElement;

const initHost = () => {
host = document.createElement('div');
host.setAttribute('id', 'host');
document.body.appendChild(host);
};
beforeEach(() => {
initHost();
});
afterEach(() => {
host.remove();
});

describe('Test: KBreadcrumbItem', () => {
test('props: cls', async () => {
const instance = new KBreadcrumbItem({
target: host,
props: {
cls: 'k-breadcrumb-item--test'
}
});
expect(instance).toBeTruthy();
expect((host as HTMLElement)!.innerHTML.includes('k-breadcrumb-item--test')).toBeTruthy();
expect(host.innerHTML).matchSnapshot();
});

test('props: href is empty', async () => {
const instance = new KBreadcrumbItem({
target: host,
props: {
href: ''
}
});
expect(instance).toBeTruthy();
expect((host as HTMLElement)!.innerHTML.includes('k-breadcrumb-item-link')).toBeFalsy();
expect(host.innerHTML).matchSnapshot();
});

test('props: href is not empty', async () => {
const instance = new KBreadcrumbItem({
target: host,
props: {
href: 'https://github.com/ikun-svelte/ikun-ui'
}
});
expect(instance).toBeTruthy();
expect((host as HTMLElement)!.innerHTML.includes('k-breadcrumb-item-link')).toBeTruthy();
expect(host.querySelector('a')?.getAttribute('href')).toBe(
'https://github.com/ikun-svelte/ikun-ui'
);
expect(host.innerHTML).matchSnapshot();
});
});
39 changes: 39 additions & 0 deletions components/BreadcrumbItem/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@ikun-ui/breadcrumb-item",
"version": "0.0.9-beta.2",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"svelte": "dist/index.js",
"types": "src/index.ts",
"keywords": [
"svelte",
"svelte3",
"web component",
"component",
"react",
"vue",
"svelte-kit",
"dx"
],
"scripts": {
"build": "npm run build:js && npm run build:svelte",
"build:js": "tsc -p . --outDir dist/ --rootDir src/",
"build:svelte": "svelte-strip strip src/ dist",
"publish:npm": "pnpm publish --no-git-checks --access public"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@ikun-ui/icon": "workspace:*",
"@ikun-ui/utils": "workspace:*",
"baiwusanyu-utils": "^1.0.14"
},
"devDependencies": {
"@tsconfig/svelte": "^5.0.0",
"svelte-strip": "^2.0.0",
"tslib": "^2.6.1",
"typescript": "^5.1.6"
}
}
31 changes: 31 additions & 0 deletions components/BreadcrumbItem/src/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script lang="ts">
import { getContext } from 'svelte';
import { KIcon } from '@ikun-ui/icon';
import { getPrefixCls, createCls, BreadcrumbKey } from '@ikun-ui/utils';
export let href: string = '';
export let cls: string = '';
export let attrs: Record<string, string> = {};
const breadcrumbProps = getContext(BreadcrumbKey) || {};
const { separator = '/', separatorIcon = '' } = breadcrumbProps;
// class names
const prefixCls = getPrefixCls('breadcrumb-item');
$: itemCls = createCls(prefixCls, cls);
$: linkCls = createCls(`${prefixCls}-content`, `${prefixCls}-content__dark`, {
[`${prefixCls}-link`]: !!href
});
$: separatorCls = createCls(`${prefixCls}-separator`, `${prefixCls}-separator__dark`);
</script>

<span class={itemCls} {...$$restProps} {...attrs}>
<svelte:element this={href ? 'a' : 'span'} class={linkCls} {href}>
<slot />
</svelte:element>
{#if separatorIcon}
<KIcon cls={separatorCls} icon={separatorIcon} width="14px" height="14px"></KIcon>
{:else}
<span class={separatorCls}>{separator}</span>
{/if}
</span>
5 changes: 5 additions & 0 deletions components/BreadcrumbItem/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="./types" />
import BreadcrumbItem from './index.svelte';
export { BreadcrumbItem as KBreadcrumbItem };

export default BreadcrumbItem;
6 changes: 6 additions & 0 deletions components/BreadcrumbItem/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference types="svelte" />
export type KBreadcrumbItemProps = {
href: string;
cls: string;
attrs: Record<string, string>;
};
11 changes: 11 additions & 0 deletions components/BreadcrumbItem/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@tsconfig/svelte/tsconfig.json",

"compilerOptions": {
"noImplicitAny": true,
"strict": true,
"declaration": true
},
"include": ["src/**/*.ts", "src/**/*.svelte"],
"exclude": ["node_modules/*", "**/*.spec.ts"]
}
10 changes: 10 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ const components = [
}
]
},
{
text: 'Navigation',
collapsed: false,
items: [
{
text: 'Breadcrumb',
link: '/components/KBreadcrumb'
}
]
},
{
text: 'Feedback',
collapsed: false,
Expand Down
67 changes: 67 additions & 0 deletions docs/components/KBreadcrumb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: KBreadcrumb
lang: en-US
---

# KBreadcrumb

Displays the location of the current page, making it easier to browser back.

## Install

::: code-group

```bash [pnpm]
pnpm add @ikun-ui/breadcrumb @ikun-ui/breadcrumb-item
```

```bash [yarn]
yarn add @ikun-ui/breadcrumb @ikun-ui/breadcrumb-item
```

```bash [npm]
npm install @ikun-ui/breadcrumb @ikun-ui/breadcrumb-item
```

:::

## Basic usage

In `KBreadcrumb`, each `KBreadcrumbItem` is a tag that stands for every level starting from homepage. This component has a String attribute `separator`, and it determines the `separator`. Its default value is '/'.

<demo src="../../../../example/breadcrumb/basic.svelte" github='Breadcrumb'></demo>

## Icon separator

Set `separatorIcon` to use `KIcon` as the separator, it will cover `separator`.

<demo src="../../../../example/breadcrumb/separator-icon.svelte" github='Breadcrumb'></demo>

## Breadcrumb Props

| Name | Type | Default | Description |
| ------------- | -------- | ------- | ---------------------------------------------------------- |
| separator | `string` | `/` | separator character |
| separatorIcon | `string` | `''` | The class name of the icon, following the unocss standard. |
| cls | `string` | `''` | Additional class for |
| attrs | `any` | `{}` | Additional attributes |

## Breadcrumb Slots

| Name | Description |
| ------- | ------------------------- |
| default | Customize default content |

## BreadcrumbItem Props

| Name | Type | Default | Description |
| ----- | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| href | `string` | `''` | The URL that the hyperlink points to, same as [href <span class="i-carbon-link text-12px" />](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a) |
| cls | `string` | `''` | Additional class for |
| attrs | `any` | `{}` | Additional attributes |

## BreadcrumbItem Slots

| Name | Description |
| ------- | ------------------------- |
| default | Customize default content |
9 changes: 9 additions & 0 deletions docs/example/breadcrumb/basic.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import { KBreadcrumb } from '@ikun-ui/breadcrumb';
import { KBreadcrumbItem } from '@ikun-ui/breadcrumb-item';
</script>

<KBreadcrumb separator=">">
<KBreadcrumbItem href="/">IKun UI</KBreadcrumbItem>
<KBreadcrumbItem>Breadcrumb</KBreadcrumbItem>
</KBreadcrumb>
9 changes: 9 additions & 0 deletions docs/example/breadcrumb/separator-icon.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
import { KBreadcrumb } from '@ikun-ui/breadcrumb';
import { KBreadcrumbItem } from '@ikun-ui/breadcrumb-item';
</script>

<KBreadcrumb separatorIcon="i-carbon:arrow-right">
<KBreadcrumbItem href="/">IKun UI</KBreadcrumbItem>
<KBreadcrumbItem>Breadcrumb</KBreadcrumbItem>
</KBreadcrumb>
Loading

0 comments on commit 8f10021

Please sign in to comment.