0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【tailwindcss】tailwindcssのクラスユーティリティをベースにスタイルを作る

Posted at

やりたかったこと

tailwindcssが用意しているtop-*をベースにして、新しいtailwindcssクラスユーティリティを作りたい。

tailwindcssが用意しているtop-*クラスユーティリティ
image.png

こんな感じでtop-*クラスユーティリティをベースとしたカスタムクラスユーティリティを作りたい
image.png

使用例
<div className="panel-top-80"></div>
出力されるcss
/*top-80をベースにして".react-flow__panel.top."というセレクタを付与してCSS出力する*/ 
.react-flow__panel.top.panel-top-80 {
    top: 20rem;
}

設定方法

tailwind.config.js

import defaultTheme from "tailwindcss/defaultTheme";
import forms from "@tailwindcss/forms";

/** @type {import('tailwindcss').Config} */
export default {
    //...省略...
    
    plugins: [
        //...省略...
        
        function ({ addUtilities, theme }) {
            const topValues = theme("spacing");

            const customTopUtilities = Object.entries(topValues).reduce(
                (acc, [key, value]) => {
                    acc[`.react-flow__panel.panel-top-${key}`] = {
                        top: value,
                    };
                    return acc;
                },
                {}
            );
            // ユーティリティを追加
            addUtilities(customTopUtilities, ["responsive"]);
        },
    ],
};

acc[.react-flow__panel.panel-top-${key}]と指定すると、最後のピリオド区切りの要素(panel-top-${key})が表示名になる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?