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

@charcoal-ui/tailwind-config

v3.12.0

Published

charcoal のカラーテーマやスペーシングなどの定数を元に tailwind.config.js を生成するライブラリです。

Downloads

2,059

Readme

@charcoal-ui/tailwind-config

charcoal のカラーテーマやスペーシングなどの定数を元に tailwind.config.js を生成するライブラリです。

インストール

npm

npm i --save-dev @charcoal-ui/tailwind-config

yarn

yarn add -D @charcoal-ui/tailwind-config

使い方

tailwind.config.js の中で require して使います。

デフォルトの config を使う

const { config } = require('@charcoal-ui/tailwind-config')

/**
 * @type {import('tailwindcss/tailwind-config').TailwindConfig}
 */
module.exports = {
  darkMode: false,
  content: ['./src/**/*.tsx', './src/**/*.html'],
  presets: [config]
};

カスタマイズする

const { light, dark } = require('@charcoal-ui/theme')
const { createTailwindConfig } = require('@charcoal-ui/tailwind-config')

/**
 * @type {import('tailwindcss/tailwind-config').TailwindConfig}
 */
module.exports = {
  darkMode: false,
  content: ['./src/**/*.tsx', './src/**/*.html'],
  presets: [
    createTailwindConfig({
      version: 'v3',
      theme: {
        ':root': light,
        '@media (prefers-color-scheme: dark)': dark
      }
    })
  ]
};

createTailwindConfig には以下のオプションを渡すことができます。

| キー | 型 | 説明 | | ------- | ----------------------- | --------------------------------------------------------------------------------------------------------------------- | | version | v1 v2 v3 | Tailwind.css のバージョン系統を指定します。色のキー名に使われる DEFAULTdefault などの違いを内部で吸収します。 | | theme | Record<string, Theme> | 条件ごとの色の値のマッピングを渡します。:root は必須のキーです。以下詳細。 |

ThemeMap について

charcoal は Tailwind.css の通常のダークモード( dark:〇〇 系のクラス )をサポートしません。

なぜなら charcoal における「色」は、同じ名前でも light テーマと dark テーマで違うカラーコードであり得るからです。

これを表現するために @charcoal-ui/tailwind-config では theme というオプションを受け取ります。

createTailwindConfig({
  version: 'v3',
  theme: {
    ':root': light,
    '@media (prefers-color-scheme: dark)': dark,
  },
})

↑ は、:root(つまり通常ケース)では light テーマを、システムの設定がダークモードである場合は dark テーマを使用することを表現します。:root は必須のキーであり、ダークテーマ対応を行わないサービスであっても、必ず自身のプロダクトのカラーテーマを渡す必要があります。

theme をこのように書くと、tailwind-config は内部的に以下のような CSS Variables を生成します。

/* 以下はイメージです */

:root {
  --tailwind-color-background1: #fff;
  --tailwind-color-background2: #f5f5f5;
  /* ... */
}

@media (prefers-color-scheme: dark) {
  :root {
    --tailwind-color-background1: #1f1f1f;
    --tailwind-color-background2: #000000;
    /* ... */
  }
}

このとき、以下のように書く必要はありません。キーが @media で始まる場合はビルド時に :root が補われます。

{
  theme: {
    // これは間違い!!
    '@media (prefers-color-scheme: dark)': {
      ':root': dark
    },

    // 正しくはこう
    '@media (prefers-color-scheme: dark)': dark
  }
}

theme のキーとして通常のセレクタを指定することもできます。これにより、ダークモードの切り替えが DOM の状態に依存するケースにも対応します。localStorage など JS での切り替えを実装しているアプリケーションはこちらの方式(何かしら CSS セレクタで表現できる方法)を使ってください。

createTailwindConfig({
  version: 'v3',
  theme: {
    ':root': light,
    'html[data-theme="dark"]': dark,
  },
})
/* 以下はイメージです */

:root {
  --tailwind-color-background1: #fff;
  --tailwind-color-background2: #f5f5f5;
  /* ... */
}

html[data-theme='dark'] {
  --tailwind-color-background1: #1f1f1f;
  --tailwind-color-background2: #000000;
  /* ... */
}

利用できるクラス

Tailwind.css に通常存在するクラスについては公式ドキュメントを見てください。

https://tailwindcss.com/

@charcoal-ui/tailwind-config が独自に定義しているクラスについては Storybook を見てください。

https://pixiv.github.io/charcoal/?path=/docs/tailwind-config-colors-doc--colors