Skip to content

Commit

Permalink
[gem] Use decorator metadata
Browse files Browse the repository at this point in the history
Closed #156
  • Loading branch information
mantou132 committed Jul 9, 2024
1 parent d55461e commit 4d9351c
Show file tree
Hide file tree
Showing 106 changed files with 701 additions and 710 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"lerna": "^8.0.0",
"lint-staged": "^15.1.0",
"prettier": "^3.1.0",
"typescript": "^5.4.3"
"typescript": "^5.5.3"
},
"resolutions": {
"**/esbuild": "^0.21.2"
"**/esbuild": "^0.23.0"
},
"lint-staged": {
"*.ts": "eslint --fix"
Expand Down
2 changes: 1 addition & 1 deletion packages/duoyun-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@web/dev-server-esbuild": "^1.0.2",
"@web/test-runner": "^0.18.1",
"rimraf": "^3.0.2",
"typescript": "^5.4.3",
"typescript": "^5.5.3",
"webpack-merge": "^5.7.3"
},
"author": "mantou132",
Expand Down
7 changes: 4 additions & 3 deletions packages/duoyun-ui/src/elements/action-text.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GemElement, html } from '@mantou/gem/lib/element';
import { adoptedStyle, customElement, attribute, state, slot } from '@mantou/gem/lib/decorators';
import { adoptedStyle, customElement, attribute, state, slot, focusable, aria } from '@mantou/gem/lib/decorators';
import { createCSSSheet, css } from '@mantou/gem/lib/utils';

import { theme, getSemanticColor } from '../lib/theme';
Expand Down Expand Up @@ -34,6 +34,8 @@ const style = createCSSSheet(css`
@customElement('dy-action-text')
@adoptedStyle(style)
@adoptedStyle(focusStyle)
@focusable()
@aria({ role: 'button' })
export class DuoyunActionTextElement extends GemElement {
@slot static unnamed: string;

Expand All @@ -42,8 +44,7 @@ export class DuoyunActionTextElement extends GemElement {
@state active: boolean;

constructor() {
super({ focusable: true });
this.internals.role = 'button';
super();
this.addEventListener('keydown', commonHandle);
}

Expand Down
8 changes: 2 additions & 6 deletions packages/duoyun-ui/src/elements/alert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// https://spectrum.adobe.com/page/in-line-alert/
import { adoptedStyle, customElement, attribute, property, slot } from '@mantou/gem/lib/decorators';
import { adoptedStyle, customElement, attribute, property, slot, aria } from '@mantou/gem/lib/decorators';
import { GemElement, html } from '@mantou/gem/lib/element';
import { createCSSSheet, css } from '@mantou/gem/lib/utils';

Expand Down Expand Up @@ -49,6 +49,7 @@ type Status = 'positive' | 'notice' | 'negative' | 'informative' | 'default';
*/
@customElement('dy-alert')
@adoptedStyle(style)
@aria({ role: 'alert' })
export class DuoyunAlertElement extends GemElement {
@slot static unnamed: string;

Expand All @@ -74,11 +75,6 @@ export class DuoyunAlertElement extends GemElement {
return getSemanticColor(this.status) || theme.neutralColor;
}

constructor() {
super();
this.internals.role = 'alert';
}

render = () => {
const icon = this.#icon;
return html`
Expand Down
2 changes: 1 addition & 1 deletion packages/duoyun-ui/src/elements/area-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class DuoyunAreaChartElement extends DuoyunChartBaseElement {
}

constructor() {
super({ isLight: false });
super();
this.addEventListener('pointermove', this.#onPointerMove);
this.addEventListener('pointerout', this.#onPointerOut);
this.addEventListener('pointercancel', this.#onPointerOut);
Expand Down
7 changes: 2 additions & 5 deletions packages/duoyun-ui/src/elements/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
boolattribute,
numattribute,
part,
aria,
} from '@mantou/gem/lib/decorators';
import { GemElement, html } from '@mantou/gem/lib/element';
import { createCSSSheet, css, exportPartsMap } from '@mantou/gem/lib/utils';
Expand Down Expand Up @@ -161,6 +162,7 @@ export type AvatarItem = {
*/
@customElement('dy-avatar-group')
@adoptedStyle(groupStyle)
@aria({ role: 'list' })
export class DuoyunAvatarGroupElement extends GemElement {
@part static avatar: string;

Expand All @@ -180,11 +182,6 @@ export class DuoyunAvatarGroupElement extends GemElement {
return this.max || this.#items?.length || 0;
}

constructor() {
super();
this.internals.role = 'list';
}

#parts = exportPartsMap({ [DuoyunAvatarElement.avatar]: DuoyunAvatarGroupElement.avatar });

#renderAvatar = ({ src = '', tooltip = '', alt = '', status, onClick }: AvatarItem) => {
Expand Down
9 changes: 4 additions & 5 deletions packages/duoyun-ui/src/elements/banner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
property,
boolattribute,
slot,
shadow,
aria,
} from '@mantou/gem/lib/decorators';
import { GemElement, html } from '@mantou/gem/lib/element';
import { createCSSSheet, css } from '@mantou/gem/lib/utils';
Expand Down Expand Up @@ -82,6 +84,8 @@ type Status = 'positive' | 'notice' | 'negative' | 'default';
*/
@customElement('dy-banner')
@adoptedStyle(style)
@shadow()
@aria({ role: 'banner' })
export class DuoyunBannerElement extends GemElement {
@slot static unnamed: string;

Expand All @@ -103,11 +107,6 @@ export class DuoyunBannerElement extends GemElement {
}
}

constructor() {
super({ delegatesFocus: true });
this.internals.role = 'banner';
}

render = () => {
const icon = this.#icon;
return html`
Expand Down
10 changes: 5 additions & 5 deletions packages/duoyun-ui/src/elements/base/chart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { adoptedStyle, emitter, Emitter, property, state, part } from '@mantou/gem/lib/decorators';
import { html, svg, TemplateResult, GemElementOptions } from '@mantou/gem/lib/element';
import { adoptedStyle, emitter, Emitter, property, state, part, aria } from '@mantou/gem/lib/decorators';
import { html, svg, TemplateResult } from '@mantou/gem/lib/element';
import { createCSSSheet, css, randomStr } from '@mantou/gem/lib/utils';

import { theme } from '../../lib/theme';
Expand Down Expand Up @@ -53,6 +53,7 @@ const style = createCSSSheet(css`
`);

@adoptedStyle(style)
@aria({ role: 'img' })
export class DuoyunChartBaseElement<_T = Record<string, unknown>> extends DuoyunResizeBaseElement {
@part static chart: string;

Expand All @@ -79,9 +80,8 @@ export class DuoyunChartBaseElement<_T = Record<string, unknown>> extends Duoyun
return this.aspectRatio || 2;
}

constructor(options?: GemElementOptions) {
super(options);
this.internals.role = 'img';
constructor() {
super();
this.memo(
() => (this.filtersSet = new Set(this.filters)),
() => [this.filters],
Expand Down
6 changes: 3 additions & 3 deletions packages/duoyun-ui/src/elements/base/loadable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { adoptedStyle, customElement, boolattribute } from '@mantou/gem/lib/decorators';
import { GemElement, html, GemElementOptions } from '@mantou/gem/lib/element';
import { GemElement, html } from '@mantou/gem/lib/element';
import { createCSSSheet, css } from '@mantou/gem/lib/utils';

import { theme } from '../../lib/theme';
Expand Down Expand Up @@ -53,8 +53,8 @@ const style = createCSSSheet(css`
export class DuoyunLoadableBaseElement<T = Record<string, unknown>> extends GemElement<T> {
@boolattribute loading: boolean;

constructor(options?: GemElementOptions) {
super(options);
constructor() {
super();
const mask = new DuoyunLoadableMaskElement();
this.effect(() => {
this.internals.ariaBusy = String(this.loading);
Expand Down
9 changes: 5 additions & 4 deletions packages/duoyun-ui/src/elements/base/resize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { emitter, Emitter } from '@mantou/gem/lib/decorators';
import { GemElement, GemElementOptions } from '@mantou/gem/lib/element';
import { GemElement } from '@mantou/gem/lib/element';

import { throttle } from '../../lib/timer';

Expand All @@ -8,7 +8,9 @@ export type ResizeDetail = {
borderBoxSize: DuoyunResizeBaseElement['borderBoxSize'];
};

export function resizeObserver(ele: DuoyunResizeBaseElement, options: { throttle?: boolean } = {}) {
export type DuoyunResizeBaseElementOptions = { throttle?: boolean };

export function resizeObserver(ele: DuoyunResizeBaseElement, options: DuoyunResizeBaseElementOptions = {}) {
const { throttle: needThrottle = true } = options;
const callback = (entryList: ResizeObserverEntry[]) => {
entryList.forEach((entry) => {
Expand All @@ -29,12 +31,11 @@ export function resizeObserver(ele: DuoyunResizeBaseElement, options: { throttle
return () => ro.disconnect();
}

export type DuoyunResizeBaseElementOptions = GemElementOptions & { throttle?: boolean };
export class DuoyunResizeBaseElement<_T = Record<string, unknown>> extends GemElement {
@emitter resize: Emitter<ResizeDetail>;

constructor(options: DuoyunResizeBaseElementOptions = {}) {
super(options);
super();
this.effect(
() => resizeObserver(this, options),
() => [],
Expand Down
6 changes: 3 additions & 3 deletions packages/duoyun-ui/src/elements/base/visible.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { emitter, Emitter, property, state } from '@mantou/gem/lib/decorators';
import { GemElement, GemElementOptions } from '@mantou/gem/lib/element';
import { GemElement } from '@mantou/gem/lib/element';

export function visibilityObserver(ele: DuoyunVisibleBaseElement) {
const io = new IntersectionObserver(
Expand Down Expand Up @@ -36,8 +36,8 @@ export class DuoyunVisibleBaseElement<_T = Record<string, unknown>> extends GemE
@property intersectionRoot?: Element | Document;
@property intersectionRootMargin?: string;

constructor(options?: GemElementOptions) {
super(options);
constructor() {
super();
this.effect(
() => visibilityObserver(this),
() => [this.intersectionRoot],
Expand Down
5 changes: 2 additions & 3 deletions packages/duoyun-ui/src/elements/base/wake-lock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**只要元素显示在屏幕内,屏幕就不会自动熄灭 */

import { GemElementOptions } from '@mantou/gem/lib/element';
import { logger } from '@mantou/gem/helper/logger';
import { addListener } from '@mantou/gem/lib/utils';

Expand Down Expand Up @@ -39,8 +38,8 @@ export function wakeLock(ele: DuoyunWakeLockBaseElement) {
}

export class DuoyunWakeLockBaseElement<_T = Record<string, unknown>> extends DuoyunVisibleBaseElement {
constructor(options?: GemElementOptions) {
super(options);
constructor() {
super();
this.effect(
() => wakeLock(this),
() => [],
Expand Down
8 changes: 2 additions & 6 deletions packages/duoyun-ui/src/elements/breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// https://spectrum.adobe.com/page/breadcrumbs/
import { adoptedStyle, customElement, property, boolattribute, part } from '@mantou/gem/lib/decorators';
import { adoptedStyle, customElement, property, boolattribute, part, aria } from '@mantou/gem/lib/decorators';
import { GemElement, html } from '@mantou/gem/lib/element';
import { createCSSSheet, css, classMap } from '@mantou/gem/lib/utils';

Expand Down Expand Up @@ -55,6 +55,7 @@ export type BreadcrumbsItem = {
*/
@customElement('dy-breadcrumbs')
@adoptedStyle(style)
@aria({ ariaLabel: 'breadcrumbs' })
export class DuoyunBreadcrumbsElement extends GemElement {
@part static item: string;

Expand All @@ -68,11 +69,6 @@ export class DuoyunBreadcrumbsElement extends GemElement {
return this.items || this.list;
}

constructor() {
super();
this.internals.ariaLabel = 'breadcrumbs';
}

render = () => {
return html`
${this.#items?.map(
Expand Down
4 changes: 3 additions & 1 deletion packages/duoyun-ui/src/elements/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
state,
part,
slot,
shadow,
} from '@mantou/gem/lib/decorators';
import { GemElement, html } from '@mantou/gem/lib/element';
import { history } from '@mantou/gem/lib/history';
Expand Down Expand Up @@ -139,6 +140,7 @@ const style = createCSSSheet(css`
@adoptedStyle(style)
@adoptedStyle(focusStyle)
@connectStore(icons)
@shadow()
export class DuoyunButtonElement extends GemElement {
@slot static unnamed: string;

Expand Down Expand Up @@ -167,7 +169,7 @@ export class DuoyunButtonElement extends GemElement {
}

constructor() {
super({ delegatesFocus: true });
super();
this.addEventListener('click', () => {
if (this.disabled) return;
if (this.route) {
Expand Down
4 changes: 3 additions & 1 deletion packages/duoyun-ui/src/elements/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
property,
boolattribute,
part,
shadow,
} from '@mantou/gem/lib/decorators';
import { GemElement, html, TemplateResult } from '@mantou/gem/lib/element';
import { createCSSSheet, css, classMap, partMap } from '@mantou/gem/lib/utils';
Expand Down Expand Up @@ -112,6 +113,7 @@ interface Day {
@customElement('dy-calendar')
@adoptedStyle(style)
@adoptedStyle(focusStyle)
@shadow()
export class DuoyunCalendarElement extends GemElement {
@part static headerRow: string;
@part static headerLeftCell: string;
Expand Down Expand Up @@ -140,7 +142,7 @@ export class DuoyunCalendarElement extends GemElement {
@property renderDate?: (date: Time) => TemplateResult;

constructor() {
super({ delegatesFocus: true });
super();
}

#isHighlightRange = (date: Time) => {
Expand Down
9 changes: 3 additions & 6 deletions packages/duoyun-ui/src/elements/card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// https://spectrum.adobe.com/page/cards/
import { adoptedStyle, customElement, attribute, property, part, slot } from '@mantou/gem/lib/decorators';
import { adoptedStyle, customElement, attribute, property, part, slot, shadow, aria } from '@mantou/gem/lib/decorators';
import { html, TemplateResult } from '@mantou/gem/lib/element';
import { createCSSSheet, css } from '@mantou/gem/lib/utils';

Expand Down Expand Up @@ -99,6 +99,8 @@ export type ActionItem = Omit<ContextMenuItem, 'handle'> & { handle: (rest: HTML
@customElement('dy-card')
@adoptedStyle(style)
@adoptedStyle(focusStyle)
@shadow()
@aria({ role: 'group' })
export class DuoyunCardElement extends DuoyunLoadableBaseElement {
@part static preview: string;
@part static avatar: string;
Expand All @@ -116,11 +118,6 @@ export class DuoyunCardElement extends DuoyunLoadableBaseElement {
@property actions?: ActionItem[];
@attribute crossorigin: 'anonymous' | 'use-credentials';

constructor() {
super({ delegatesFocus: true });
this.internals.role = 'group';
}

#onOpenMenu = (evt: MouseEvent) => {
const target = evt.target as HTMLElement;
ContextMenu.open(
Expand Down
6 changes: 2 additions & 4 deletions packages/duoyun-ui/src/elements/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
part,
emitter,
Emitter,
shadow,
} from '@mantou/gem/lib/decorators';
import { createCSSSheet, css, styleMap, classMap, addListener } from '@mantou/gem/lib/utils';

Expand Down Expand Up @@ -150,6 +151,7 @@ type State = {
@customElement('dy-carousel')
@adoptedStyle(style)
@adoptedStyle(focusStyle)
@shadow()
export class DuoyunCarouselElement extends GemElement<State> {
@part static img: string;
@part static title: string;
Expand All @@ -175,10 +177,6 @@ export class DuoyunCarouselElement extends GemElement<State> {
return this.interval || 3000;
}

constructor() {
super({ delegatesFocus: true });
}

state: State = {
currentIndex: 0,
direction: 1,
Expand Down
Loading

0 comments on commit 4d9351c

Please sign in to comment.