Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ブロックテーマ時代における、テーマの CSS について考える Toro_Unit / 202...

ブロックテーマ時代における、テーマの CSS について考える Toro_Unit / 2025.09.13 @ Shinshu WordPress Meetup

信州 WordPress Meetup in 松本 Vol.37 WordPress とかの話をする会 ( https://www.meetup.com/shinshu-wordpress-meetup/events/310575290 )

Avatar for Toro_Unit (Hiroshi Urabe)

Toro_Unit (Hiroshi Urabe)

September 13, 2025
Tweet

More Decks by Toro_Unit (Hiroshi Urabe)

Other Decks in Technology

Transcript

  1. $ whoami Toro_Unit / 占部 紘 Frontend & WordPress Engineer

    Github: @torounit Twitter: @Toro_Unit 2
  2. Contributions WordPress / Gutenberg Team Shinshu WordPress Meetup WordCamp Tokyo

    2023 Speaker WordCamp Kansai 2024 Organizer Team WordCamp Kansai 2025 Organizer Team 3
  3. 4

  4. theme.json 昔よりかなりいろんなことができるようになった。 margin / padding / gap / border typography

    color / background border-radius / box-shadow etc... https://make.wordpress.org/core/2025/03/12/roster-of- design-tools-per-block-wordpress-6-8-edition/ 8
  5. Section Styles ( WordPress 6.6+ ) ブロックのバリエーションを、 json で定義できるように。その定 義にも、

    theme.json のようなス タイルのプロパティが使える。 子ブロックのスタイルも定義でき る。 https://make.wordpress.org/core/2024/ 06/24/section-styles/ 9
  6. 簡単なモノであれば、theme.json で記述出来るが... { "css": "& .wp-block-button__link::after { content: '→'; }"

    } この時点でつらい。 しかもブロックごとのCSSには、パーサーの関係で メディアクエリ等など様々な機能が使えない。 & th, &td {} とかも使えない。 14
  7. コアのブロックへのスタイルをCSSファイルで記述すれば解決! wp_enqueue_block_style( 'core/button', array( 'handle' => 'my-core-button-default', 'src' => get_parent_theme_file_uri(

    'styles/blocks/core/button/default.css' ), 'path' => get_parent_theme_file_path( 'styles/blocks/core/button/default.css' ), ) ); wp_enqueue_block_style( 'core/button', array( 'handle' => 'my-core-button-variation', 'src' => get_parent_theme_file_uri( 'styles/blocks/core/button/variation.css' ), 'path' => get_parent_theme_file_path( 'styles/blocks/core/button/variation.css' ), ) ); 15
  8. 30

  9. 31

  10. 32

  11. フロントエンドの世界では、コンポーネント単位でCSSを 管理するのが一般的になって早数年 Vue.js / Svelte / Riot.js ...etc CSS-in-JS Styled

    Components Emotion CSS Modules Gutenberg も React で記述されており、管理画面のコンポーネントにも このような技術が用いられている。 33
  12. 35

  13. 36

  14. 38

  15. 39

  16. package.json { "private": true, "scripts": { "build": "wp-scripts build --webpack-copy-php",

    "dev": "wp-scripts start --webpack-copy-php" }, "devDependencies": { "@tailwindcss/postcss": "^4.1.11", "@wordpress/scripts": "^30.20.0", "tailwindcss": "^4.1.11" } } 43
  17. src/theme.css Tailwind CSS の preflight を読み込むと WordPressのスタイルが壊れるので 除外。 https://tailwindcss.com/docs/preflight#disabling-preflight @layer

    theme, base, components, utilities; @import "tailwindcss/theme.css" layer(theme); @import "tailwindcss/utilities.css" layer(utilities); パターンやら、パーツやら、テンプレートやらを対象にする。 @source "../{parts,templates,patterns}/**/*.{php,html}"; 45
  18. おまけ blockごとのCSSを別のファイルに分割したときも それらの CSS で @apply つかって Tailwind CSS を使いたいとき。

    .wp-block-button__link { @apply relative; &::after { @apply absolute top-1/2 right-3 -translate-y-1/2; content: '→'; } } 48
  19. webpack.config.js で頑張れば出来る。ふだんこんな感じでやってる。 const defaultConfig = require("@wordpress/scripts/config/webpack.config"); const RemoveEmptyScriptsPlugin = require("webpack-remove-empty-scripts");

    const path = require("path"); const glob = require("glob"); /* 各ブロックのCSSファイルをentryに追加するためのオブジェクトを作成*/ const blockStyleDir = "src/styles"; const blockEntries = glob .sync("blocks/**/*.css", { cwd: blockStyleDir }) .map((key) => [`styles/${key.replace(".css", "")}`, path.resolve(process.cwd(), blockStyleDir, key)]); const blockEntriesObj = Object.fromEntries(blockEntries); module.exports = { ...defaultConfig, entry: { ...defaultConfig.entry, "styles/theme/theme": path.resolve(process.cwd(), "src/styles/theme/theme.css"), ...blockEntriesObj, }, plugins: [ ...defaultConfig.plugins, new RemoveEmptyScriptsPlugin({ stage: RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS, }), ], watchOptions: { ignored: ["**/node_modules", "**/build/**"], }, }; 49
  20. ワークフロー 1. npm run dev で wp-scripts start 2. ブラウザで確認しながら、パターンやパーツの編集

    3. WordPress の機能で難しいものは追加クラスを書く。 4. create-block-theme の機能でテーマファイルを更新。 面倒なときは、直接ファイルを弄るときもよくある。 50
  21. 感想 全部 Tailwind CSS で書くと管理画面上での変更が効かなかったり等が発生 するので、WordPress 側で出来ることはそちらで。足りないところを Tailwind CSS で補う感じ。

    まぁなんだかんだこんな感じで1年くらいやってみているけど、今のとこ ろ上手くいってる感じがする。この CSS どこで使ってるの?みたいな話 はだいぶ無くなった。 Tailwind CSS v4 になって、設定ファイルが不要になったのも楽で良い。 ブロックの追加CSSをいじると壊れるのはどーせ一緒なので気にしない。 壊れたらパターンをリセットすれば解決する。 52