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

vue-component-style

v1.1.0

Published

A Vue mixin to add style section to components.

Downloads

25

Readme

Vue Component Style

npm npm NPM

A Vue mixin to add style section to components.

Features

  • Zero Dependency
  • Tiny (~1kb gzipped)
  • Simple Setup and Usage
  • Nested Support
  • Pseudo Selector Support
  • SSR Support
  • Scoped to Component

Install

npm i vue-component-style

Setup

Vue App

// Vue 2.x
import Vue from 'vue';
import VueComponentStyle from 'vue-component-style';

Vue.use(VueComponentStyle);

// Vue 3.x (added support in >=1.1.0)
import { createApp } from 'vue';
import VueComponentStyle from 'vue-component-style';

const theApp = createApp(/*...*/);
theApp.use(VueComponentStyle);

Note that You should use version >= 1.1.0 to use in Vue 3.

Nuxt App

nuxt.config.js:

module.exports = {
  modules: [
    'vue-component-style/nuxt'
  ],
}

Note that You don't need to do anything else with your webpack config or whatever.

Usage

component.vue:

<template>
  <div>
    <h1 :class="$style.title"> Title </h1>
    <div :class="$style.content">
      Content <a> Link </a>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    size: {
      type: Number,
      default: 8
    },
    color: {
      type: String,
      default: 'red'
    }
  },
  style({ className }) {
    return [
      className('title', {
        color: this.color,
        fontWeight: 'bold',
      }),
      className('content', {
        color: 'gray',
        marginBottom: `${this.size}px`,
        '& > a': {
          color: this.color,
          '&:visited': {
            textDecoration: 'underline',
          },
        },
      }),
    ];
  }
}
</script>

API Documentions

1 - Define Style

Function this.style(helper)

After activating VueComponentStyle, all components can have their js style section. Just like data section, you have to pass normal function that returning an Array. This function will invoke automatically with helper util object as first argument.

2 - Use Defined Styles

Object this.$style

After you defining style prop in your component, all your classes defined by style()s are accessable with $style computed object inside your component instance.

3 - Notice When Styles Updated

VueEvent 'styleChange'

styleChange event fires when your style changes and applied to DOM.


Helper

You can use helper() object from first parameter of style() function to defining your stylesheet. Helper object has these functions

Class Name

Function helper.className(name, content)

To define your scopped css class styles, use this helper function.

| Param | Type | Default | Description | | - | - | - | - | | name | String | | Name of your class. All of your defined names will be accessable via $style Object later. | | content | Object | {} | Your sass-style class properties. You can also style nested. |

Example
style({ className }) {
  return [
    className('customClass', {
      color: 'red',
      fontWeight: 'bold',
      borderRadius: `${this.size}px`,
      '& > div': {
        color: 'blue',
      },
    }),
  ];
}

Media Query

Function helper.mediaQuery(mediaFeature, content)

To define your customized style to different screen sizes, use this helper function.

| Param | Type | Default | Description | | - | - | - | - | | mediaFeature | Object | | Media features. Common keys on this object are 'minWidth' and 'maxWidth'. | | content | Array | [] | List of className()s that you need to redefine. |

Example
style({ mediaQuery, className }) {
  return [
    className('responsiveClass', {
      width: '50%',
    }),
    mediaQuery({ maxWidth: '320px' }, [
      className('responsiveClass', {
        width: '100%',
      }),     
    ]),
  ];
}

Key Frames

Function helper.keyFrames(name, content)

To define your scopped keyframes animation with specefic name, use this helper function.

| Param | Type | Default | Description | | - | - | - | - | | name | String | | Keyframes name. | | content | Object | | Keyframes properties. If you don't pass this prop, calculated hash name of already generated keyframes will be returns. |

Example
style({ keyFrames, className }) {
  return [
    className('animatedThing', {
      color: 'blue',
      animationName: keyFrames('myAnimation'),
      animationDuration: '2s',
    }),
    keyFrames('myAnimation', {
      from: {
        color: 'blue',
      },
      to: {
        color: 'red',
      },
    ]),
  ];
}

Custom

Function helper.custom(rule, content)

To define your custom css style sections, use this helper function. Note that styles generated by this helper function are not scopped!

| Param | Type | Default | Description | | - | - | - | - | | rule | String | | Rule name. | | content | Object | | Style properties. |

Example
style({ custom }) {
  return [
    custom('@font-face', {
      fontFamily: 'MyFont',
      src: 'url("/my-font.woff")',
    }),
  ];
}