Skip to content

Commit

Permalink
[gem] Share styles
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Jul 29, 2024
1 parent 11bab76 commit c2dd559
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
38 changes: 20 additions & 18 deletions packages/gem/src/elements/base/link.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GemElement, html } from '../../lib/element';
import { attribute, property, state, part, connectStore, shadow, aria } from '../../lib/decorators';
import { createCSSSheet, GemElement, html } from '../../lib/element';
import { attribute, property, state, part, connectStore, shadow, aria, adoptedStyle } from '../../lib/decorators';
import { history, basePathStore } from '../../lib/history';
import { absoluteLocation } from '../../lib/utils';
import { absoluteLocation, css } from '../../lib/utils';
import { ifDefined } from '../../lib/directives';

import { matchPath, RouteItem, RouteOptions, createHistoryParams, createPath } from './route';
Expand All @@ -10,6 +10,22 @@ function isExternal(path: string) {
return !path.startsWith('/');
}

const styles = createCSSSheet(css`
:host {
/* link default style */
cursor: pointer;
color: blue;
text-decoration: underline;
}
a {
all: unset;
display: contents;
}
a:visited {
color: unset;
}
`);

/**
* @customElement gem-link
* @attr href
Expand All @@ -25,6 +41,7 @@ function isExternal(path: string) {
* Bug: print `<link>` https://github.com/mantou132/gem/issues/36
*/
@connectStore(basePathStore)
@adoptedStyle(styles)
@shadow()
@aria({ focusable: true })
export class GemLinkElement extends GemElement {
Expand Down Expand Up @@ -125,21 +142,6 @@ export class GemLinkElement extends GemElement {

render() {
return html`
<style>
:host {
/* link default style */
cursor: pointer;
color: blue;
text-decoration: underline;
}
a {
all: unset;
display: contents;
}
a:visited {
color: unset;
}
</style>
<a
part=${this.link}
@click=${this.#preventDefault}
Expand Down
29 changes: 16 additions & 13 deletions packages/gem/src/elements/base/use.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { GemElement, html } from '../../lib/element';
import { attribute, property, shadow, state } from '../../lib/decorators';
import { createCSSSheet, GemElement, html } from '../../lib/element';
import { adoptedStyle, attribute, property, shadow, state } from '../../lib/decorators';
import { css } from '../../lib/utils';

const styles = createCSSSheet(css`
:host(:where(:not([hidden]))) {
position: relative;
display: inline-flex;
align-items: center;
}
svg {
width: 100%;
height: 100%;
}
`);

const eleCache = new Map<string, HTMLTemplateElement>();
/**
Expand All @@ -12,6 +25,7 @@ const eleCache = new Map<string, HTMLTemplateElement>();
* @attr selector
*/
@shadow()
@adoptedStyle(styles)
export class GemUseElement extends GemElement {
@attribute selector: string; // CSS 选择器
@property root?: HTMLElement | Document | ShadowRoot;
Expand All @@ -38,17 +52,6 @@ export class GemUseElement extends GemElement {

render() {
return html`
<style>
:host(:where(:not([hidden]))) {
position: relative;
display: inline-flex;
align-items: center;
}
svg {
width: 100%;
height: 100%;
}
</style>
${this.#getContent()}
<slot></slot>
`;
Expand Down

0 comments on commit c2dd559

Please sign in to comment.