Skip to content

Commit 484964c

Browse files
committed
feat: use css cascade layer to control style conveniently
1 parent 943dfa9 commit 484964c

File tree

9 files changed

+55
-1
lines changed

9 files changed

+55
-1
lines changed

docs/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"preview": "astro preview",
1010
"test": "echo \"Error: no test specified\" && exit 1"
1111
},
12+
"browserslist": [
13+
"fully supports css-cascade-layers"
14+
],
1215
"dependencies": {
1316
"astro": "catalog:build",
1417
"astro-friday": "workspace:*"

packages/astro-friday/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
"peerDependencies": {
4141
"@astrojs/mdx": "catalog:deps",
4242
"@astrojs/sitemap": "catalog:seo",
43+
"@byronogis/postcss-global-data": "catalog:deps",
4344
"@iconify-json/lucide": "catalog:icons",
4445
"@vercel/og": "catalog:seo",
4546
"astro": "catalog:build",
4647
"astro-seo": "catalog:seo",
4748
"dayjs": "catalog:deps",
4849
"pixi.js": "catalog:frontend",
50+
"postcss-preset-env": "catalog:deps",
4951
"rehype-parse": "catalog:unified",
5052
"rehype-remark": "catalog:unified",
5153
"remark-gfm": "catalog:unified",

packages/astro-friday/src/config.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import type { ArtConfig, NavItem, ProjectItem } from './types'
1515
import type { CollectionEntry } from './types/content'
1616
import type { Appearance } from './utils/appearance'
1717
import path from 'node:path'
18+
import { fileURLToPath } from 'node:url'
1819
import { defu } from 'defu'
1920

21+
const _dirname = path.dirname(fileURLToPath(import.meta.url))
22+
2023
type GlobOptions = Parameters<typeof glob>[0]
2124

2225
export function getDefaultConfig(userConfig: Config, astroConfig: AstroConfig): Config {
@@ -127,6 +130,21 @@ export function getDefaultConfig(userConfig: Config, astroConfig: AstroConfig):
127130
viewTransition: {
128131
enable: true,
129132
},
133+
postcss: {
134+
postcssGlobalData: {
135+
files: [
136+
{
137+
file: path.resolve(_dirname, './styles/global-data.css'),
138+
remove: false,
139+
position: 'prepend',
140+
},
141+
],
142+
},
143+
postcssPresetEnv: {
144+
stage: 2,
145+
minimumVendorImplementations: 2,
146+
},
147+
},
130148
integrations: {
131149
nprogress: {
132150
showSpinner: false,
@@ -482,6 +500,13 @@ export interface Config {
482500
*/
483501
enable?: boolean
484502
}
503+
/**
504+
* Built-in PostCSS plugins configuration options
505+
*/
506+
postcss?: {
507+
postcssGlobalData?: import('@byronogis/postcss-global-data').pluginOptions
508+
postcssPresetEnv?: import('postcss-preset-env').pluginOptions
509+
}
485510
/**
486511
* Integrations configuration, you can configure or disable built-in integrations here.
487512
*/
@@ -572,6 +597,9 @@ export type ResolvedConfig = SetRequiredDeep<
572597
| 'components.NavbarBrand'
573598
| 'viewTransition'
574599
| 'viewTransition.enable'
600+
| 'postcss'
601+
| 'postcss.postcssGlobalData'
602+
| 'postcss.postcssPresetEnv'
575603
| 'integrations'
576604
| 'integrations.nprogress'
577605
| 'integrations.sitemap'

packages/astro-friday/src/integration.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { AstroIntegration, InjectedRoute } from 'astro'
55
import type { Config } from './config'
66
import path from 'node:path'
77
import { fileURLToPath } from 'node:url'
8+
import postcssGlobalData from '@byronogis/postcss-global-data'
9+
import postcssPresetEnv from 'postcss-preset-env'
810
import Inspect from 'vite-plugin-inspect'
911
import { resolveConfig } from './config'
1012
import { mdx } from './integrations/mdx'
@@ -57,6 +59,14 @@ export function integration(userConfig: Config = {}): AstroIntegration {
5759
vitePluginAstroFridayComponents(resolvedConfig),
5860
Inspect(),
5961
],
62+
css: {
63+
postcss: {
64+
plugins: [
65+
postcssGlobalData(resolvedConfig.postcss.postcssGlobalData),
66+
postcssPresetEnv(resolvedConfig.postcss.postcssPresetEnv),
67+
],
68+
},
69+
},
6070
},
6171
})
6272

packages/astro-friday/src/integrations/unocss/uno.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import {
1313
import { presetScrollbar } from 'unocss-preset-scrollbar'
1414

1515
export default defineConfig({
16+
outputToCssLayers: {
17+
cssLayerName(layer) {
18+
return layer === 'default'
19+
? 'uno-default'
20+
: `uno`
21+
},
22+
},
1623
shortcuts: [
1724
{
1825
'hoverable-text': 'opacity-60 hover:opacity-100 transition-(opacity 200 ease)',

packages/astro-friday/src/layouts/Basic.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import config from 'virtual:astro-friday-config'
55
import Art from '../components/Art/Art.astro'
66
import HeadContent from '../components/HeadContent.astro'
77
import 'virtual:astro-friday-unocss-extract'
8-
import '../styles/main.css'
8+
import '../styles/index.css'
99
1010
interface Props {
1111
seo?: import('astro-seo').Props
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@layer uno, astro, astro-friday, uno-default;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "./main.css" layer(astro-friday);

pnpm-workspace.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ catalogs:
77
astro: ^5.14.8
88
deps:
99
'@astrojs/mdx': ^4.3.7
10+
'@byronogis/postcss-global-data': ^1.0.1
1011
astro-color-scheme: ^1.1.5
1112
astro-integration-kit: ^0.19.1
1213
c12: ^3.3.1
1314
dayjs: ^1.11.18
1415
defu: ^6.1.4
1516
jiti: ^2.6.1
17+
postcss-preset-env: ^10.4.0
1618
type-fest: ^5.1.0
1719
typescript: ^5.9.3
1820
unocss: ^66.5.4

0 commit comments

Comments
 (0)