All Projects → sidx1024 → svelte-style-directive

sidx1024 / svelte-style-directive

Licence: MIT License
A custom Svelte preprocessor to add support for style directive.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to svelte-style-directive

svelte-typescript
Typescript monorepo for Svelte v3 (preprocess, template, types)
Stars: ✭ 214 (+1026.32%)
Mutual labels:  preprocessor, svelte
svelte-themer
A theming engine for your Svelte apps using CSS Variables, persisted.
Stars: ✭ 88 (+363.16%)
Mutual labels:  svelte, css-variables
Stylable
Stylable - CSS for components
Stars: ✭ 1,109 (+5736.84%)
Mutual labels:  preprocessor, style
Svelte Css Vars
✨ Ever wanted to have reactive css variables in svelte? What if I told you there's a way?
Stars: ✭ 150 (+689.47%)
Mutual labels:  svelte, css-variables
css-render
Generating CSS using JS with considerable flexibility and extensibility, at both server side and client side.
Stars: ✭ 137 (+621.05%)
Mutual labels:  preprocessor, style
modern-fluid-typography-editor
Modern fluid typography editor
Stars: ✭ 222 (+1068.42%)
Mutual labels:  svelte
svelte-router
Router component for Svelte
Stars: ✭ 63 (+231.58%)
Mutual labels:  svelte
terminal-style
🎨 Return your terminal message in style! Change the text style, text color and text background color from the terminal, console or shell interface with ANSI color codes. Support for Laravel and Composer.
Stars: ✭ 16 (-15.79%)
Mutual labels:  style
safenetwork-gitportal
p2p git portal - a decentralised alternative to github
Stars: ✭ 12 (-36.84%)
Mutual labels:  svelte
clui
A command system with extra steps
Stars: ✭ 18 (-5.26%)
Mutual labels:  svelte
memento-svelte-electron-typescript
Template to create a desktop app with Svelte, TailwindCSS, Electron and TypeScript (with electron-updater, electron-reload and electron-builder)
Stars: ✭ 27 (+42.11%)
Mutual labels:  svelte
sveltober
Cybernetically enhanced October applications
Stars: ✭ 19 (+0%)
Mutual labels:  svelte
cv
My online CV using Svelte
Stars: ✭ 35 (+84.21%)
Mutual labels:  svelte
svelte-generic-crud-table
Agnostic web-component for object-arrays with CRUD functionality.
Stars: ✭ 38 (+100%)
Mutual labels:  svelte
svelte-pdf
svelte-pdf provides a component for rendering PDF documents using PDF.js
Stars: ✭ 102 (+436.84%)
Mutual labels:  svelte
react-cx
Combine styles from CSS Modules with a `cx` prop.
Stars: ✭ 24 (+26.32%)
Mutual labels:  style
svelteit
Svelteit is a minimalistic UI/UX component library for Svelte and Sapper projects
Stars: ✭ 64 (+236.84%)
Mutual labels:  svelte
HandySub
Download Subtitle from Subscene and other sources
Stars: ✭ 42 (+121.05%)
Mutual labels:  style
http-interceptors
The Web apps in this monorepo make HTTP requests and require uniform consistency in how they are executed and handled. This monorepo demonstrates the same app written with Angular and with Svelte. Each app uses HTTP interceptors. The Angular app uses HttpClient and its interceptors while the Svelte app uses Axios and its interceptors.
Stars: ✭ 46 (+142.11%)
Mutual labels:  svelte
svelte-trivia
A Quiz app completely made using Svelte
Stars: ✭ 25 (+31.58%)
Mutual labels:  svelte

Archive Note

Archived because Svelte added support for style directives.

npm version

What is it?

A custom Svelte preprocessor to add support for style directive.

<span
  style:font-size="16px"
  style:color={color}
  style:text-transform="lowercase"
  >

Usage

Add to package.json

npm i --save-dev svelte-style-directive

Add to rollup.config.js

import svelte from 'rollup-plugin-svelte'
import { svelteStyleDirective } from 'svelte-style-directive'

export default {
  plugins: [
    svelte({
      preprocess: [
        svelteStyleDirective()
      ]
    })
  ]
}

Why?

It's very convenient to apply classes based on state/prop in Svelte.

<script>
  let hidden = false;
  let bold = true;
</script>

<style>
  .hidden {
    display: none;
  }
  .bold {
    font-weight: bold;
  }
</style>

<span class:hidden={hidden} class:bold={bold}>Heading</span>

class directive makes things much easier. But, what if you wanted to make this work with style attributes? This plugin adds support style directive to achieve similar functionality.

So you can do this:

<script>
  let bold = true;
  let color = 'red';
</script>

<span style:font-weight={bold} style:color={color}>Heading</span>

instead of this:

<script>
  let bold = true;
  let color = 'red';
</script>

<span style={`${bold ? 'font-weight: bold; ' : ''}${color ? 'color: red; ' : ''}`}>Heading</span>

It also works for CSS Variables!

Performance

As this is a preprocessor, it is just a syntactic sugar. It has no runtime overhead.

Examples

Using state

<script>
  let progress = 0.5;
</script>

<!-- Without style directive -->
<div class="progress-bar">
  <div class="cursor" style={`left: ${progress * 100 + '%'};`}></div>
</div>

<!-- With style directive -->
<div class="progress-bar">
  <div class="cursor" style:left={progress * 100 + '%'}></div>
</div>

<!-- Assume styles for progress-bar and cursor are already declared -->

CSS variables

<script>
  let textColor = '#9c9c9c';
</script>

<style>
  span {
    color: var(--text-color);
  }
</style>

<div style:--text-color={textColor}>
  <span>Some text with color</span>
</div>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].