Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-months-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chialab/dna": minor
---

Add support for importing `globalStyles`.
40 changes: 0 additions & 40 deletions .github/workflows/lint_build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,43 +106,3 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage/clover.xml

test-browserstack:
name: Test Browserstack
runs-on: ubuntu-latest
concurrency: browserstack
strategy:
max-parallel: 2
matrix:
browser:
- 'chrome-latest'
- 'chrome-latest-1'
- 'chrome-latest-2'
- 'chrome-87'
- 'firefox-latest'
- 'firefox-latest-1'
- 'firefox-latest-2'
- 'firefox-90'
- 'safari-latest'
- 'safari-16'
- 'safari-15'
- 'edge-latest'
steps:
- name: Checkout the repository
uses: actions/checkout@v6

- name: Setup Node
uses: actions/setup-node@v6
with:
cache: yarn

- name: Install project dependencies
run: yarn install
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true

- name: Run tests
run: yarn test:browserstack --browser browserstack:${{ matrix.browser }}
env:
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
40 changes: 40 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,43 @@ jobs:
lint-build-test:
uses: ./.github/workflows/lint_build_test.yml
secrets: inherit

test-browserstack:
name: Test Browserstack
runs-on: ubuntu-latest
concurrency: browserstack
strategy:
max-parallel: 2
matrix:
browser:
- 'chrome-latest'
- 'chrome-latest-1'
- 'chrome-latest-2'
- 'chrome-87'
- 'firefox-latest'
- 'firefox-latest-1'
- 'firefox-latest-2'
- 'firefox-90'
- 'safari-latest'
- 'safari-16'
- 'safari-15'
- 'edge-latest'
steps:
- name: Checkout the repository
uses: actions/checkout@v6

- name: Setup Node
uses: actions/setup-node@v6
with:
cache: yarn

- name: Install project dependencies
run: yarn install
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true

- name: Run tests
run: yarn test:browserstack --browser browserstack:${{ matrix.browser }}
env:
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/2.2.0/schema.json",
"$schema": "https://biomejs.dev/schemas/2.3.11/schema.json",
"files": {
"includes": [
"**",
Expand Down
46 changes: 43 additions & 3 deletions docs/guide/styling.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
# Styles
# Styling

## Global styles

You can define global styles for your components using the static `globalStyles` property. This property can be a string, an array of strings or a `CSSStyleSheet` instance.

::: warning

Be careful when using global styles, as they will affect the entire document and may lead to unexpected results if not managed properly.
Always use a [style technique](#other-styling-techniques) that scopes your CSS rules to avoid collisions.

:::

When a component is defined, its global styles are injected into the document `<head>` inside a `<style>` tag (or adopted if a `CSSStyleSheet` is used).

```tsx
import { Component, customElement } from '@chialab/dna';

@customElement('x-card')
class Card extends Component {
static globalStyles = `
x-card {
display: block;
}
`;
}
```

### Importing CSS modules

You can also import CSS as `CSSStyleSheet` instances if your bundler supports it.

```tsx
import { Component, customElement } from '@chialab/dna';
import styles from './x-card.css' with { type: 'css' };

@customElement('x-card')
class Card extends Component {
static globalStyles = styles;
}
```

## Scoped styles

DNA can render plain `<style>` tags in a template, but what about style encapsulation?

Expand All @@ -10,8 +52,6 @@ You may don't need to use DNA's scoped style if you are already using a styling

:::

## Scoped styles

Every `<style>` tag rendered to a component is scoped.

```tsx
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
"playwright": "^1.50.0",
"preact": "beta",
"publint": "^0.3.0",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react": "~19.1.0",
"react-dom": "~19.1.0",
"rimraf": "^6.0.0",
"rxjs": "^7.8.2",
"svelte": "^5.33.1",
Expand Down
10 changes: 10 additions & 0 deletions src/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export const extend = <T extends HTMLElement, C extends Constructor<HTMLElement>
return attributes;
}

/**
* Component global stylesheets.
*/
static readonly globalStyles?: string | CSSStyleSheet | (CSSStyleSheet | string)[];

/**
* Define component properties.
*/
Expand Down Expand Up @@ -801,6 +806,11 @@ export interface BaseComponentConstructor<T extends HTMLElement = HTMLElement> {
*/
readonly observedAttributes?: string[];

/**
* Component stylesheets.
*/
readonly globalStyles?: string | CSSStyleSheet | (CSSStyleSheet | string)[];

/**
* Define component properties.
*/
Expand Down
Loading
Loading