Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: typescript and css preprocessor usage #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
63 changes: 60 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,62 @@ import { styleChildComponent } from "svelte-preprocess-style-child-component";
}
```

### Overwriting CSS component rules

By default, the specificity of CSS component rules outweighs the specificity of the rules later applied. Anonymous layering can circumvent this behaviour, allowing to overwrite properties.

```svelte
<!-- Child.svelte -->

<style>
@layer {
p {
color: red;
}
}
</style>
```

For more information on [layering](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer), see the MDN Web Docs.

### Depending on a CSS preprocessor (e.g. Sass)

```shell
npm i -D svelte-sequential-preprocessor svelte-preprocess-style-child-component
```

```js
import seqPreprocessor from 'svelte-sequential-preprocessor';
import { styleChildComponent } from "svelte-preprocess-style-child-component";
...
{
preprocess: seqPreprocessor([preprocess(), styleChildComponent()]), // Ensure styleChildComponent is called after the CSS preprocessor
}
```

### Typescript

Create a Typescript declaration file and reference it in your `tsconfig.json`.

```ts
// e.g. ./types/svelte-preprocess-style-child-component.d.ts

declare namespace svelteHTML {
interface HTMLAttributes {
part?: string | boolean;
}
}
```

> **Warning**
> If modifying your `tsconfig.json`, objects are merged, but arrays are overwritten. Thus, it is necessary to manually merge files and directories referenced in `./.svelte-kit/tsconfig.json`.

```json
{
"include": ["./types/**/*.d.ts"]
}
```

## Additional features

### Class selector
Expand Down Expand Up @@ -116,8 +172,8 @@ It transforms component selectors to global selectors, and passes down the class
⬇️

```html
<div class={$$props.parts$$?.default$$}>
<h1 class={$$props.parts$$?.heading}>Child component!</h1>
<div class="{$$props.parts$$?.default$$}">
<h1 class="{$$props.parts$$?.heading}">Child component!</h1>
</div>
```

Expand Down Expand Up @@ -148,7 +204,8 @@ It transforms component selectors to global selectors, and passes down the class
import Child from "./Child.svelte";
</script>

<Child parts$$={{"default$$":"svelte-child-27gw8r","heading":"svelte-child-qzjtt3"}} />
<Child
parts$$={{"default$$":"svelte-child-27gw8r","heading":"svelte-child-qzjtt3"}} />

<style>
:global(.svelte-child-27gw8r) {
Expand Down