npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sveltesheet

v0.5.3

Published

A Svelte preprocessor making dynamic styles easier to use

Downloads

7

Readme

sveltesheet

A Svelte preprocessor making dynamic styles easier to use.

Important!

This preprocessor is still under development and currently doesn't support all css features, like @media. Please report any problems and missing css features.

Examples

Using variables inside css:

<!-- Flex.svelte -->
<script>
    import { css } from "sveltesheet";
    export let direction: "column" | "row" = "row";
</script>

<div>
    <slot />
</div>

{css`
    div {
        display: flex;
        flex-direction: ${direction};
    }
`}

Referencing Svelte components in css:

<!-- Box.svelte -->
<script>
    import { css } from "sveltesheet";
    export let color = "red";
</script>

<Flex>
    <slot />
</Flex>

${css`
    ${Flex} {
        background-color: ${color};
    }
`}

In order for sveltesheet to know which tag we actually want to target when we write ${Flex}, we need to add {...cssTarget} on the target tag inside Flex.svelte:

<!-- Flex.svelte -->
<script>
    import { css, cssTarget } from "sveltesheet";
    export let direction: "column" | "row" = "row";
</script>

<div {...cssTarget}>
    <slot />
</div>

{css`
    div {
        display: flex;
        flex-direction: ${direction};
    }
`}

Using bigger css parts

Sometimes it's useful to reuse a part of css. With sveltesheet, you can use the @css at-rule or css property to achieve this.

css property

<script>
    import { css } from "sveltesheet";

    function getTextStyle(size: "big" | "small") {
        // Note that you can use the `css` function for convenient syntax highlighting with a variety of IDE-plugins
        return css`
            font-size: ${size === "big" ? 2 : 1}rem;
            font-weight: ${size === "big" ? 700 : 400};
        `;
    }

</script>

<h1>Big heading!</h1>
<h2>Small heading.</h2>

{css`
    h1 {
        css: ${getTextStyle("big")};
    }
    h2 {
        css: ${getTextStyle("small")};
    }
`}

@css at-rule

With the @css at rule, you can create whole rules, including the selectors.

💥 Important! The resulting css will always have a global scope, as it is not processed at runtime.

<script>
    import { css } from "sveltesheet";

    // Note that you can use the `css` function for convenient syntax highlighting with a variety of IDE-plugins
    const redTextCss = css`
        p {
            color: red;
        }
    `;
</script>

<h1>Title!</h1>
<p>Some text</p>

{css`
    @css ${redTextCss};
`}

How to use

  1. Install the package. Run:
    • npm install stylesheet
  2. Inside svelte.config.js, add sveltesheet() to the list of preprocessors:
import adapter from "@sveltejs/adapter-auto";
import { vitePreprocess } from "@sveltejs/kit/vite";
//Import this:
import { sveltesheet } from "sveltesheet";

const config = {
	//Add `sveltesheet()` here:
	preprocess: [sveltesheet(), vitePreprocess()],
	kit: {
		adapter: adapter(),
	},
};

export default config;

That's it! Sveltesheet should now be ready to use. ✨

How does it work?

There is no magic. This is a simple preprocessor, turning your css code into a <style> or <svelte:element this="style"> tag, depending on if your css is "dynamic" or not.

This...

{css`
    ${Component} > div {
        width: ${200 + 300}px;
        height: 200px;
    }

    div {
        background-color: red;
    }
`}

gets transformed into something similar to this:

<style>
    [data-sveltesheet-ids="componentid"] > div {
        height: 200px;
    }
    div {
        background-color: red;
    }
</style>

<svelte:element this="style">
{`
    [data-sveltesheet-ids="componentid"][data-sveltesheet-ids="runtimeId"] > div[data-sveltesheet-ids="runtimeId"] {
        width: ${200 + 300}px;
    }
`}
</svelte:element>

The preprocessor knows when you're using dynamic or static properties and splits them into <style> or <svelte:element this="style"> accordingly. Static properties get put into the <style>-tag, while dynamic properties go to into the <svelte:element>-tag.

This way, the css inside <svelte:element> gets dynamically updated each time its dependencies change, while the style-tag stays static at all times.