Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/metonym/svelte-style

Style utilities as Svelte actions
https://github.com/metonym/svelte-style

style svelte svelte-action utility

Last synced: 25 days ago
JSON representation

Style utilities as Svelte actions

Awesome Lists containing this project

README

        

# svelte-style

[![NPM][npm]][npm-url]

> Style utilities as Svelte actions

`svelte-style` provides Svelte actions that conditionally apply styles as an alternative to writing CSS.

Try it in the [Svelte REPL](https://svelte.dev/repl/f759197ad49348b1bb4e181e735e08bc).

---

## Installation

**Yarn**

```bash
yarn add -D svelte-style
```

**NPM**

```bash
npm i -D svelte-style
```

**pnpm**

```bash
pnpm i -D svelte-style
```

## Available actions

### Visually hidden

Use the `visuallyHidden` action to hide content without breaking screen readers.

```svelte

import { visuallyHidden } from "svelte-style";

Text

```

The action accepts an argument that conditionally toggles the style.

```svelte

import { visuallyHidden } from "svelte-style";

let toggled = false;

(toggled = !toggled)}>
Toggle Text

```

## API

### Style class

Create your own Svelte action that conditionally applies styles using the `Style` class.

**JavaScript**

```svelte no-eval

import { Style } from "svelte-style";

const style = "color: red";

const colorRed = (node, enabled) => new Style(node, enabled, style).init();

Red text

```

**TypeScript**

If your set-up includes TypeScript, use `UseStyleType` to statically type the action.

```ts
import { Style } from "svelte-style";
import { UseStyleType } from "svelte-style/src/Style";

const style = "color: red";

const colorRed: UseStyleType = (node, enabled) => new Style(node, enabled, style).init();
```

## TypeScript

Svelte version 3.31 or greater is required to use this component with TypeScript.

TypeScript definitions are located in the [types folder](types/).

## Changelog

[CHANGELOG.md](CHANGELOG.md)

## License

[MIT](LICENSE)

[npm]: https://img.shields.io/npm/v/svelte-style.svg?style=for-the-badge&color=%23ff3e00
[npm-url]: https://npmjs.com/package/svelte-style