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

@iel/varx

v1.3.3

Published

A css-var function extension.

Downloads

76

Readme

Varx 介绍

通过 js 开发的一个 css var polyfill ,让开发者可以在低浏览器(ie)项目中使用 css var 来编写样式,更有利于换肤功能的开发。

安装方式

项目安装

npm i -S @iel/varx
// or
yarn add @iel/varx

项目使用(Vue)

Vue2

import Vue from 'vue'
import { createVarx } from '@iel/varx'
import GenerateRootStyle from '@iel/varx/lib/plugins/generate-root-style'
import LowBrowser from '@iel/varx/lib/plugins/low-browser'
import InVue from '@iel/varx/lib/plugins/in-vue'

const varx = createVarx({
  plugins: [
    // 生成 :root 样式
    GenerateRootStyle(),
    // 只在不支持 `css var` 的浏览器中开启兼容功能
    LowBrowser({ onlyLowBrowser: process.env.NODE_ENV === 'production' })
  ]
})

// 挂载在全局上便于使用
Vue.prototype.$varx = InVue(varx)

Vue3

vue3 中推荐使用 hooks 方式进行使用。

import { createVarx } from '@iel/varx'
import GenerateRootStyle from '@iel/varx/lib/plugins/generate-root-style'
import LowBrowser from '@iel/varx/lib/plugins/low-browser'
import InVue from '@iel/varx/lib/plugins/in-vue'

const varx = createVarx({
  plugins: [
    // 生成 :root 样式
    GenerateRootStyle(),
    // 只在不支持 `css var` 的浏览器中开启兼容功能
    LowBrowser({ onlyLowBrowser: process.env.NODE_ENV === 'production' })
  ]
})

export default function useVarx() {
  return InVue(varx)
}

使用方式

Vue2

<template>
  <div id="app">
    更换主题色:
    <input :value="$varx.grv('primaryColor')" type="color" @input="onColorChange" />
    <textarea :style="`background: ${$varx.grv('primaryColor')};`" />
    <button :style="buttonStyle">Submit</button>
  </div>
</template>

<script>
export default {
  name: 'App',
  computed: {
    buttonStyle() {
      return {
        color: this.$varx.grv('primaryColor')
      }
    }
  },
  methods: {
    onColorChange(e) {
      // 注册 `css var`
      this.$varx.encodeVarObj({
        primaryColor: {
          value: e.target.value,
          onUpdate: (value, key, varx, eventType) => {
            // eventType: update, remove
            // 变更类型为 `remove` 时同步删除 `color`
            if (eventType === 'remove') {
              // 删除时不再触发变更事件
              return varx.deleteVar('color', false)
            }
            // 当 `primaryColor` 变更时变更 `color`,并且不再触发变更事件
            this.$varx.encodeVarTuple('color', value, false)
          }
        }
      })
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  text-align: center;
  // css 内部直接使用
  color: var(--primaryColor);
}
</style>

Vue3

<template>
  更换主题色:
  <input :value="varx.grv('primaryColor')" type="color" @input="onColorChange" />
  <textarea :style="`background: ${varx.grv('primaryColor')};`" />
  <button :style="buttonStyle">Submit</button>
</template>

<script setup>
import useVarx from '@/hooks/use-varx'
import { computed } from 'vue'

const buttonStyle = computed(() => ({ color: varx.grv('primaryColor') }))

function onColorChange(e) {
  // 注册 `css var`
  varx.encodeVarObj({
    primaryColor: {
      value: e.target.value,
      onUpdate: (value, key, varx, eventType) => {
        // eventType: update, remove
        // 变更类型为 `remove` 时同步删除 `color`
        if (eventType === 'remove') {
          // 删除时不再触发变更事件
          return varx.deleteVar('color', false)
        }
        // 当 `primaryColor` 变更时变更 `color`,并且不再触发变更事件
        varx.encodeVarTuple('color', value, false)
      }
    }
  })
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  text-align: center;
  // css 内部直接使用
  color: var(--primaryColor);
}
</style>