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

smart-skin

v1.0.5

Published

js for web change skin

Downloads

8

Readme

背景:做了一年的App突然需要加主题切换的功能,App中有大量的h5页面,如果一次性全量替换其开发周期会很长,且全量发布出问题的概率会比较大;另有一些跳转比较深的页面不需要做主题切换功能,但是这些页面可能和需要做主题色的页面是同一个单页面,所以主题切换需要做到路由级别。

基础实现方案

需要和设计师沟通好,对设计师的设计规范有较高要求,主题切换仅仅是改变样色,icon等

css定义主题类型

  1. 定义全局主题色分别对应各个主题色
// theme=white/night/blue
html[data-theme="white"]
  1. 根据主题色定义全局的主题变量
html[data-theme="white"]:root {
    --background-color: #fff;
    --text-color: black;
}
html[data-theme="night"]:root {
    --background-color: black;
    --text-color: #fff;
}
html[data-theme="blue"]:root {
    --background-color: blue;
    --text-color: #fff;
}
  1. 项目开发中颜色都用变量代替
.app-container {
    background: var(--primariy-color);
}

通过js改变全局的data-theme属性的方式来切换主题

我这里写了一个插件实现主题初始化和开发环境下模拟切换主题色

源码参见: https://github.com/xiejun-net/smart-skin

import SmartSkin from 'smart-skin'

const smartSkin = new SmartSkin({
    skinTypeList: ['white', 'night', 'blue'],
    currentSkin: 'white',
    darkSkinType: 'night',
    lightSkinStype: 'white',
    followUpSystem: false,
    debugger: true, 
})

// 皮肤初始化
smartSkin.init()

跟随系统的处理,只支持macOS/iOS/iPad

if (this.options.followUpSystem) {
    const matchMedia = window.matchMedia('(prefers-color-scheme: dark)')
    this._followUpSystem(matchMedia)
    matchMedia.addEventListener('change', this._followUpSystem)
}

private _followUpSystem(ev: MediaQueryListEvent | MediaQueryList) {
        // 跟随系统
    this.changeSkinType(
        ev.matches ? this.options.darkSkinType : this.options.lightSkinStype
    )
}

支持本地调试切换主题

debugger设置为true即可 demo

xxx.gif

适配终端

App内适配

app定制webview的userAgent,增加skinType/white,H5通过读取webview中的skinType给currentSkin赋默认值

const getUaValue = key => {
    const reg = new RegExp(`(^|\\s)${key}\\/[^\\s]+`)
    const match = navigator.userAgent.match(reg)
    return match ? match[0].split('/')[1] : ''
}
currentSkin = getUaValue('skinType')

App内部部分路由需要开启主题,在路由meta中添加needTheme的属性,当为true的时候初始化,否则移除data-theme

if (to.meta.needTheme) {
    smartSkin.init()
} else {
    smartSkin.removeAllSkinType()
}

App外部

function getUrlParam(name) {
    let RegExpObject = new RegExp(
        '[?&]' + encodeURIComponent(name) + '=([^&|#]*)'
    )
    if ((name = RegExpObject.exec(window.location.href)))
        return decodeURIComponent(name[1])
}
currentSkin = getUrlParam('skinType')