Skip to content

Commit

Permalink
[duoyun-ui] Add <dy-badge>
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Dec 25, 2023
1 parent f073dfb commit 45e71e0
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 73 deletions.
2 changes: 1 addition & 1 deletion packages/duoyun-ui/docs/en/01-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DuoyunUI aims to provide a UI library for lightweight, high performance, full fe
- Action
> [`<dy-action-text>`](../02-elements/action-text.md), [`<dy-button>`](../02-elements/button.md), [`<dy-copy>`](../02-elements/copy.md), [`<dy-link>`](../02-elements/link.md), [`<dy-contextmenu>`](../02-elements/contextmenu.md), [`<dy-options>`](../02-elements/options.md)
- Feedback
> [`<dy-coach-mark>`](../02-elements/coach-mark.md), [`<dy-drawer>`](../02-elements/drawer.md), [`<dy-help-text>`](../02-elements/help-text.md), [`<dy-input-capture>`](../02-elements/input-capture.md), [`<dy-keyboard-access>`](../02-elements/keyboard-access.md), [`<dy-loading>`](../02-elements/loading.md), [`<dy-meter>`](../02-elements/meter.md), [`<dy-modal>`](../02-elements/modal.md), [`<dy-page-loadbar>`](../02-elements/page-loadbar.md), [`<dy-placeholder>`](../02-elements/placeholder.md), [`<dy-popover>`](../02-elements/popover.md), [`<dy-progress>`](../02-elements/progress.md), [`<dy-status-light>`](../02-elements/status-light.md), [`<dy-toast>`](../02-elements/toast.md), [`<dy-tooltip>`](../02-elements/tooltip.md), [`<dy-wait>`](../02-elements/wait.md)
> [`<dy-badge>`](../02-elements/badge.md), [`<dy-coach-mark>`](../02-elements/coach-mark.md), [`<dy-drawer>`](../02-elements/drawer.md), [`<dy-help-text>`](../02-elements/help-text.md), [`<dy-input-capture>`](../02-elements/input-capture.md), [`<dy-keyboard-access>`](../02-elements/keyboard-access.md), [`<dy-loading>`](../02-elements/loading.md), [`<dy-meter>`](../02-elements/meter.md), [`<dy-modal>`](../02-elements/modal.md), [`<dy-page-loadbar>`](../02-elements/page-loadbar.md), [`<dy-placeholder>`](../02-elements/placeholder.md), [`<dy-popover>`](../02-elements/popover.md), [`<dy-progress>`](../02-elements/progress.md), [`<dy-status-light>`](../02-elements/status-light.md), [`<dy-toast>`](../02-elements/toast.md), [`<dy-tooltip>`](../02-elements/tooltip.md), [`<dy-wait>`](../02-elements/wait.md)
- Navigator
> [`<dy-breadcrumbs>`](../02-elements/breadcrumbs.md), [`<dy-pagination>`](../02-elements/pagination.md), [`<dy-side-navigation>`](../02-elements/side-navigation.md)
- Data Entry
Expand Down
37 changes: 37 additions & 0 deletions packages/duoyun-ui/docs/en/02-elements/badge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# `<dy-badge>`

Dependence on [anchor positioning](//www.w3.org/TR/css-anchor-position-1)

## Example

<gbp-example name="dy-badge" src="https://jspm.dev/duoyun-ui/elements/badge,https://jspm.dev/duoyun-ui/elements/avatar">

```json
[
{
"dot": true,
"innerHTML": "This is Link"
},
{
"count": 5,
"innerHTML": "<dy-avatar src=https://api.dicebear.com/5.x/bottts-neutral/svg></dy-avatar>"
},
{
"icon": "icons.delete",
"color": "informative",
"innerHTML": "<dy-avatar src=https://api.dicebear.com/5.x/bottts-neutral/svg></dy-avatar>"
},
{
"count": 199,
"max": 99,
"color": "positive",
"innerHTML": "<dy-avatar src=https://api.dicebear.com/5.x/bottts-neutral/svg></dy-avatar>"
}
]
```

</gbp-example>

## `<dy-badge>` API

<gbp-api name="dy-badge" src="/src/elements/badge.ts"></gbp-api>
2 changes: 1 addition & 1 deletion packages/duoyun-ui/docs/zh/01-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ DuoyunUI 是使用 [Gem](https://gemjs.org/) 开发的桌面端中后台 Web UI
- 动作元素
> [`<dy-action-text>`](../02-elements/action-text.md), [`<dy-button>`](../02-elements/button.md), [`<dy-copy>`](../02-elements/copy.md), [`<dy-link>`](../02-elements/link.md), [`<dy-contextmenu>`](../02-elements/contextmenu.md), [`<dy-options>`](../02-elements/options.md)
- 反馈元素
> [`<dy-coach-mark>`](../02-elements/coach-mark.md), [`<dy-drawer>`](../02-elements/drawer.md), [`<dy-help-text>`](../02-elements/help-text.md), [`<dy-input-capture>`](../02-elements/input-capture.md), [`<dy-keyboard-access>`](../02-elements/keyboard-access.md), [`<dy-loading>`](../02-elements/loading.md), [`<dy-meter>`](../02-elements/meter.md), [`<dy-modal>`](../02-elements/modal.md), [`<dy-page-loadbar>`](../02-elements/page-loadbar.md), [`<dy-placeholder>`](../02-elements/placeholder.md), [`<dy-popover>`](../02-elements/popover.md), [`<dy-progress>`](../02-elements/progress.md), [`<dy-status-light>`](../02-elements/status-light.md), [`<dy-toast>`](../02-elements/toast.md), [`<dy-tooltip>`](../02-elements/tooltip.md), [`<dy-wait>`](../02-elements/wait.md)
> [`<dy-badge>`](../02-elements/badge.md), [`<dy-coach-mark>`](../02-elements/coach-mark.md), [`<dy-drawer>`](../02-elements/drawer.md), [`<dy-help-text>`](../02-elements/help-text.md), [`<dy-input-capture>`](../02-elements/input-capture.md), [`<dy-keyboard-access>`](../02-elements/keyboard-access.md), [`<dy-loading>`](../02-elements/loading.md), [`<dy-meter>`](../02-elements/meter.md), [`<dy-modal>`](../02-elements/modal.md), [`<dy-page-loadbar>`](../02-elements/page-loadbar.md), [`<dy-placeholder>`](../02-elements/placeholder.md), [`<dy-popover>`](../02-elements/popover.md), [`<dy-progress>`](../02-elements/progress.md), [`<dy-status-light>`](../02-elements/status-light.md), [`<dy-toast>`](../02-elements/toast.md), [`<dy-tooltip>`](../02-elements/tooltip.md), [`<dy-wait>`](../02-elements/wait.md)
- 导航元素
> [`<dy-breadcrumbs>`](../02-elements/breadcrumbs.md), [`<dy-pagination>`](../02-elements/pagination.md), [`<dy-side-navigation>`](../02-elements/side-navigation.md)
- 内容展示元素
Expand Down
1 change: 1 addition & 0 deletions packages/duoyun-ui/docs/zh/02-elements/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
| [`<dy-alert>`](./alert.md) | [`<Alert>`][antd-alert] | [`<ele-alert>`][ele-alert] |
| [`<dy-area-chart>`](./area-chart.md) | | |
| [`<dy-avatar>`](./avatar.md) | [`<Avatar>`][antd-avatar] | [`<ele-avatar>`][ele-avatar] |
| [`<dy-badge>`](./badge.md) | [`<Badge>`][antd-badge] | [`<ele-badge>`][ele-badge] |
| [`<dy-banner>`](./banner.md) | | |
| [`<dy-bar-chart>`](./bar-chart.md) | | |
| [`<dy-breadcrumbs>`](./breadcrumbs.md) | [`<Breadcrumb>`][antd-breadcrumb] | [`<ele-breadcrumb>`][ele-breadcrumb] |
Expand Down
37 changes: 37 additions & 0 deletions packages/duoyun-ui/docs/zh/02-elements/badge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# `<dy-badge>`

依赖[锚点定位](//www.w3.org/TR/css-anchor-position-1)

## Example

<gbp-example name="dy-badge" src="https://jspm.dev/duoyun-ui/elements/badge,https://jspm.dev/duoyun-ui/elements/avatar">

```json
[
{
"dot": true,
"innerHTML": "This is Link"
},
{
"count": 5,
"innerHTML": "<dy-avatar src=https://api.dicebear.com/5.x/bottts-neutral/svg></dy-avatar>"
},
{
"icon": "icons.delete",
"color": "informative",
"innerHTML": "<dy-avatar src=https://api.dicebear.com/5.x/bottts-neutral/svg></dy-avatar>"
},
{
"count": 199,
"max": 99,
"color": "positive",
"innerHTML": "<dy-avatar src=https://api.dicebear.com/5.x/bottts-neutral/svg></dy-avatar>"
}
]
```

</gbp-example>

## `<dy-badge>` API

<gbp-api name="dy-badge" src="/src/elements/badge.ts"></gbp-api>
5 changes: 2 additions & 3 deletions packages/duoyun-ui/scripts/hack-gbp-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ fs.writeFileSync(
fs.readFileSync(path.resolve(__dirname, '../../gem-book/src/plugins/example.ts'), { encoding: 'utf-8' }).replace(
`const script = document.createElement('script');`,
`
if (new URL(this.src).pathname.startsWith('/duoyun-ui/')) {
await import(\`../../duoyun-ui/src/elements/\${this.src.split('/').pop()}\`);
this.setState({ loading: false, error: false });
if (new URL(src).pathname.startsWith('/duoyun-ui/')) {
res(import(\`../../duoyun-ui/src/elements/\${src.split('/').pop()}\`));
return;
}
const script = document.createElement('script');`,
Expand Down
122 changes: 122 additions & 0 deletions packages/duoyun-ui/src/elements/badge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
adoptedStyle,
customElement,
attribute,
part,
slot,
numattribute,
property,
boolattribute,
refobject,
RefObject,
state,
} from '@mantou/gem/lib/decorators';
import { GemElement, html } from '@mantou/gem/lib/element';
import { classMap, createCSSSheet, css } from '@mantou/gem/lib/utils';

import { contentsContainer } from '../lib/styles';
import { getSemanticColor, theme } from '../lib/theme';
import { StringList } from '../lib/types';
import { getAssignedElements } from '../lib/element';

import './use';

const style = createCSSSheet(css`
.badge {
display: flex;
align-items: center;
justify-content: center;
font-size: 0.75em;
white-space: nowrap;
border: 2px solid currentColor;
border-radius: 100em;
color: ${theme.backgroundColor};
height: 1.5em;
min-width: 1.5em;
}
.long {
padding-inline: 0.5em;
}
:host([small]) .badge {
font-size: 0.56em;
}
:host([dot]) .badge {
height: 0.5em;
min-width: 0.5em;
}
.icon {
width: 1em;
}
@supports (anchor-name: --foo) {
.badge {
position: absolute;
anchor-default: --anchor;
top: anchor(top);
right: anchor(right);
transform: translate(50%, -50%);
}
:host(:where(:where([data-inline], :state(inline)):not([hidden]))) {
display: inline;
anchor-name: --anchor;
}
::slotted(*) {
anchor-name: --anchor;
}
}
`);

/**
* @customElement dy-badge
*/
@customElement('dy-badge')
@adoptedStyle(style)
@adoptedStyle(contentsContainer)
export class DuoyunBadgeElement extends GemElement {
@slot static unnamed: string;

@part static badge: string;

@attribute color: StringList<'positive' | 'informative' | 'notice'>;
@numattribute count: number;
@boolattribute dot: boolean;
@boolattribute small: boolean;
@numattribute max: number;

@state inline: boolean;

@property icon?: string | Element | DocumentFragment;

@refobject slotRef: RefObject<HTMLSlotElement>;

get #max() {
return this.max || 99;
}

get #color() {
return getSemanticColor(this.color) || this.color || theme.negativeColor;
}

mounted = () => {
this.slotRef.element?.addEventListener('slotchange', () => {
this.inline = !getAssignedElements(this.slotRef.element!).length;
});
};

render = () => {
const value = this.count > this.#max ? `${this.#max}+` : `${this.count}`;
return html`
<slot ref=${this.slotRef.ref}></slot>
${this.count || this.icon || this.dot
? html`
<span
class=${classMap({ badge: true, long: value.length > 1 })}
part=${DuoyunBadgeElement.badge}
style="background:${this.#color}"
>
${this.dot ? '' : this.icon ? html`<dy-use class="icon" .element=${this.icon}></dy-use>` : value}
</span>
`
: ''}
`;
};
}
9 changes: 2 additions & 7 deletions packages/duoyun-ui/src/elements/compartment.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { adoptedStyle, customElement, property } from '@mantou/gem/lib/decorators';
import { GemElement, html, TemplateResult } from '@mantou/gem/lib/element';
import { createCSSSheet, css } from '@mantou/gem/lib/utils';

const style = createCSSSheet(css`
:host(:where(:not([hidden]))) {
display: contents;
}
`);
import { contentsContainer } from '../lib/styles';

/**
* @customElement dy-compartment
*/
@customElement('dy-compartment')
@adoptedStyle(style)
@adoptedStyle(contentsContainer)
export class DuoyunCompartmentElement extends GemElement {
@property content?: string | number | TemplateResult;
render = () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/duoyun-ui/src/elements/input-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import { createCSSSheet, css } from '@mantou/gem/lib/utils';
import { theme } from '../lib/theme';
import { getDisplayKey } from '../lib/hotkeys';
import { throttle } from '../lib/utils';
import { contentsContainer } from '../lib/styles';

import './paragraph';

const style = createCSSSheet(css`
:host(:where(:not([hidden]))) {
display: contents;
}
.container,
.circle {
position: fixed;
Expand Down Expand Up @@ -50,6 +48,7 @@ type State = {
*/
@customElement('dy-input-capture')
@adoptedStyle(style)
@adoptedStyle(contentsContainer)
export class DuoyunInputCaptureElement extends GemElement<State> {
@part static container: string;
@part static kbd: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/duoyun-ui/src/elements/keyboard-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createCSSSheet, css, styleMap } from '@mantou/gem/lib/utils';
import { hotkeys, HotKeyHandles, unlock } from '../lib/hotkeys';
import { isNotNullish } from '../lib/types';
import { theme } from '../lib/theme';
import { contentsContainer } from '../lib/styles';

import { Toast } from './toast';

Expand All @@ -13,7 +14,6 @@ import './paragraph';

const style = createCSSSheet(css`
:host {
display: contents;
font-size: 0.75em;
}
.container {
Expand Down Expand Up @@ -61,6 +61,7 @@ function getChars(index: number) {
*/
@customElement('dy-keyboard-access')
@adoptedStyle(style)
@adoptedStyle(contentsContainer)
export class DuoyunKeyboardAccessElement extends GemElement<State> {
@part static kbd: string;
@attribute activekey: string;
Expand Down
9 changes: 2 additions & 7 deletions packages/duoyun-ui/src/elements/popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ import { toggleActiveState, getAssignedElements, getBoundingClientRect } from '.
import { sleep, setBodyInert } from '../lib/utils';
import { hotkeys } from '../lib/hotkeys';
import { theme } from '../lib/theme';
import { contentsContainer } from '../lib/styles';

import './reflect';

const offset = 12;

const style = createCSSSheet(css`
:host(:where(:not([hidden]))) {
display: contents;
}
`);

export type PopoverState = {
open: boolean;
left: number;
Expand Down Expand Up @@ -68,7 +63,7 @@ type CloseCallback = {
* @attr trigger
*/
@customElement('dy-popover')
@adoptedStyle(style)
@adoptedStyle(contentsContainer)
export class DuoyunPopoverElement extends GemElement<PopoverState> {
@boolattribute debug: boolean;
@boolattribute unreachable: boolean;
Expand Down
11 changes: 1 addition & 10 deletions packages/duoyun-ui/src/elements/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,7 @@ export class DuoyunResultElement extends GemElement {

render = () => {
return html`
${this.icon
? html`
<style>
.icon {
color: ${this.#color};
}
</style>
<dy-use class="icon" .element=${this.icon}></dy-use>
`
: ''}
${this.icon ? html`<dy-use class="icon" style="color:${this.#color}" .element=${this.icon}></dy-use>` : ''}
${this.illustrator ? html`<dy-use class="illustrator" .element=${this.illustrator}></dy-use>` : ''}
${this.header ? html`<dy-heading lv="2" class="header">${this.header}</dy-heading>` : ''}
${this.description ? html`<dy-paragraph class="description">${this.description}</dy-paragraph>` : ''}
Expand Down
Loading

0 comments on commit 45e71e0

Please sign in to comment.