Skip to content

Commit 9ebe095

Browse files
authored
Merge pull request #3 from pepabo/feat/default-flavor-baseline
feat: default pepper baseline + rename all.css to styles.css
2 parents 53bd1b4 + ab04d64 commit 9ebe095

4 files changed

Lines changed: 48 additions & 18 deletions

File tree

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,39 @@ npm install react react-dom react-aria-components
2424

2525
## 使い方
2626

27-
アプリのルートで `FlavorProvider` を配置し、全フレーバー同梱の CSS を読み込みます。`flavor` prop は省略時 `pepper` が適用されます。
27+
CSS をインポートするだけで、デフォルトの `pepper` フレーバーでコンポーネントが描画されます。
28+
29+
```tsx
30+
import { Button } from 'wip-ui'
31+
import 'wip-ui/styles.css'
32+
33+
export const App = () => <Button>クリック</Button>
34+
```
35+
36+
別のフレーバーを適用したい場合、あるいは領域ごとに異なるフレーバーを切り替えたい場合は `FlavorProvider` で対象領域を囲みます。
2837

2938
```tsx
3039
import { FlavorProvider, Button } from 'wip-ui'
31-
import 'wip-ui/css/all.css'
40+
import 'wip-ui/styles.css'
3241

3342
export const App = () => (
34-
<FlavorProvider>
43+
<FlavorProvider flavor="minne">
3544
<Button>クリック</Button>
3645
</FlavorProvider>
3746
)
3847
```
3948

40-
別のフレーバーに切り替える場合は `flavor` prop を指定します。ネストして領域ごとに異なるフレーバーを適用することもできます。
41-
42-
```tsx
43-
<FlavorProvider flavor="minne">
44-
<Button>クリック</Button>
45-
</FlavorProvider>
46-
```
47-
4849
コンポーネント単位でのインポートも可能です。
4950

5051
```tsx
5152
import { Button } from 'wip-ui/Button'
5253
```
5354

55+
> **中の `FlavorProvider` で切り替える場合の注意**: フレーバーを入れ子にしたとき、CSS の詳細度が同じためソース順序が後ろ(`pepper` < `minne` < `apollo` < `nachiguro` < `flippers` < `kung-pu` < `lolipop` の順)のフレーバーが勝ちます。「外側 `lolipop` + 内側 `pepper`」のような組み合わせでは、内側が外側を上書きできない点にご注意ください。
56+
5457
### 利用可能なフレーバー
5558

56-
`wip-ui/css/all.css` は以下の全フレーバーを含みます。
59+
`wip-ui/styles.css` は以下の全フレーバーを含みます。
5760

5861
- `pepper`(デフォルト)
5962
- `minne`

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
},
4545
"./types": null,
4646
"./utils": null,
47+
"./styles.css": "./dist/styles.css",
4748
"./css/*.css": "./dist/css/*.css"
4849
},
4950
"files": [

scripts/build-flavor-css.mjs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const NODE_MODULES = path.join(ROOT, 'node_modules')
99
const ENTRY_SCSS = path.join(ROOT, 'src', '_all.scss')
1010
const OUTPUT_DIR = path.join(ROOT, '.storybook', 'flavors')
1111
const DIST_DIR = path.join(ROOT, 'dist', 'css')
12+
const DIST_ROOT = path.join(ROOT, 'dist')
1213
const ICON_FONT_PATH = path.join(
1314
NODE_MODULES,
1415
'@pepabo-inhouse',
@@ -34,6 +35,9 @@ const iconFontFace = (() => {
3435
// token declarations no longer live at document level. Required for the
3536
// combined (`all.css`) build to avoid the last-flavor-wins cascade conflict
3637
// when multiple flavors' `:root { --token: ... }` blocks are concatenated.
38+
// When `prefix` is an empty string, non-:root selectors are left unchanged
39+
// so the output acts as an unscoped baseline (used to give consumers
40+
// styling even without <FlavorProvider>).
3741
function prefixPlugin(prefix, { scopeRoot = false } = {}) {
3842
return {
3943
postcssPlugin: 'prefix-flavor',
@@ -52,15 +56,20 @@ function prefixPlugin(prefix, { scopeRoot = false } = {}) {
5256
if (sel.startsWith(':root')) {
5357
return scopeRoot ? prefix + sel.slice(':root'.length) : sel
5458
}
55-
return `${prefix} ${sel}`
59+
return prefix ? `${prefix} ${sel}` : sel
5660
})
5761
},
5862
}
5963
}
6064
prefixPlugin.postcss = true
6165

66+
// Clean stale CSS outputs so renames/removals don't leave leftovers in dist/
67+
fs.rmSync(DIST_DIR, { recursive: true, force: true })
68+
fs.rmSync(path.join(DIST_ROOT, 'styles.css'), { force: true })
69+
6270
fs.mkdirSync(OUTPUT_DIR, { recursive: true })
6371
fs.mkdirSync(DIST_DIR, { recursive: true })
72+
fs.mkdirSync(DIST_ROOT, { recursive: true })
6473

6574
console.log(`Building flavor CSS for ${FLAVORS.length} flavors...`)
6675

@@ -136,18 +145,34 @@ for (const flavor of FLAVORS) {
136145
]).process(css, { from: undefined })
137146
combinedParts.push(combined.css)
138147

148+
// Baseline: emit pepper with no prefix at the top of all.css so consumers
149+
// get styled components even without wrapping with <FlavorProvider>.
150+
// Specificity of `[data-flavor="xxx"] .foo` (0,0,1,1) beats `.foo`
151+
// (0,0,1,0), so an explicit FlavorProvider still overrides the baseline.
152+
if (flavor === 'pepper') {
153+
const baseline = postcss([prefixPlugin('')]).process(css, {
154+
from: undefined,
155+
})
156+
combinedParts.unshift(baseline.css)
157+
}
158+
139159
console.log(` ✓ ${flavor}.css`)
140160
} catch (err) {
141161
console.error(` ✗ ${flavor}: ${err.message}`)
142162
process.exit(1)
143163
}
144164
}
145165

146-
// Combined CSS: single @font-face at the top, then all 7 flavors
166+
// Combined CSS: @font-face + pepper baseline (unscoped) + 7 flavors scoped.
167+
// Emitted at dist/styles.css to match modern convention
168+
// (e.g. Mantine's `@mantine/core/styles.css`, Radix Themes' `@radix-ui/themes/styles.css`)
169+
// and to signal "this is the default stylesheet consumers should import".
147170
fs.writeFileSync(
148-
path.join(DIST_DIR, 'all.css'),
171+
path.join(DIST_ROOT, 'styles.css'),
149172
iconFontFace + combinedParts.join('\n')
150173
)
151-
console.log(` ✓ all.css (combined, ${FLAVORS.length} flavors)`)
174+
console.log(
175+
` ✓ styles.css (pepper baseline + ${FLAVORS.length} flavors scoped)`
176+
)
152177

153178
console.log('Done!')

src/FlavorProvider/FlavorProvider.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ export interface Props {
1818

1919
/**
2020
* 配下の wip-ui コンポーネントに適用するフレーバー(デザイントークンのテーマ)を切り替えるための Provider。
21-
* 単一フレーバーを root で固定する用途のほか、ネストして領域ごとに異なるフレーバーを適用することもできる。
22-
* 利用前に `wip-ui/css/all.css`(全フレーバー同梱)または `wip-ui/css/{flavor}.css`(単一フレーバー)の import が必要。
21+
* `wip-ui/styles.css` をインポートするだけで pepper がデフォルト適用されるため、
22+
* この Provider はフレーバーを切り替えたい場合(別フレーバーの明示指定・領域別ネスト等)にのみ使用する。
23+
* 利用前に `wip-ui/styles.css`(全フレーバー同梱)または `wip-ui/css/{flavor}.css`(単一フレーバー)の import が必要。
2324
*
2425
* @summary フレーバー(デザイントークンのテーマ)を切り替えるProvider
2526
*/

0 commit comments

Comments
 (0)