Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle themes with objects #48

Merged
merged 7 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ version: 2.1
defaults: &defaults
working_directory: ~/postcss-themed
docker:
- image: circleci/node:8.14-browsers
- image: circleci/node:10-browsers
environment:
TZ: "/usr/share/zoneinfo/America/Los_Angeles"
TZ: '/usr/share/zoneinfo/America/Los_Angeles'

aliases:
# Circle related commands
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ language: node_js
cache: yarn
node_js:
- node
- "10"
- "8"
- "6"
- '10'
- '8'
- '6'
129 changes: 82 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ yarn test
```js
const config = {
default: {
color: 'white'
color: 'white',
},
other: {
color: 'black'
}
color: 'black',
},
};
```

Expand All @@ -62,24 +62,24 @@ or for per theme `light` and `dark` modes:
const config = {
default: {
light: {
color: 'white'
color: 'white',
},
dark: {
color: 'black'
}
color: 'black',
},
},
// Can still just have one level which defaults to light
other: {
color: 'purple'
color: 'purple',
},
more: {
light: {
color: 'red'
color: 'red',
},
dark: {
color: 'blue'
}
}
color: 'blue',
},
},
};
```

Expand Down Expand Up @@ -113,6 +113,45 @@ See [PostCSS] docs for examples for your environment.
}
```

### Theming with Objects

You can even keep theme values in deeply nested objects.

**Input:**

```js
const config = {
default: {
colors: {
primary: 'white',
},
},
other: {
colors: {
primary: 'black',
},
},
};
```

```css
.foo {
color: @theme colors.primary;
}
```

**Output:**

```css
.foo {
color: white;
}

.other .foo {
color: black;
}
```

### Component themes

Define a component level theme in either commonjs or typescript. A file names `themes.(js|ts)` must be co-located with the themeable CSS file.
Expand All @@ -137,11 +176,11 @@ import { Theme } from '@your/themes';

const CardTheme = (theme: Theme): Theme => ({
default: {
border: '1px solid red'
border: '1px solid red',
},
other: {
border: `1px solid ${theme.other.color}`
}
border: `1px solid ${theme.other.color}`,
},
});

export default CardTheme;
Expand All @@ -158,10 +197,10 @@ customize the lookup behavior by supplying your own theme resolution function.
postcss([
require('postcss-themed')({
config,
resolveTheme: cssFileName => {
resolveTheme: (cssFileName) => {
// return a function like the ones above
}
})
},
}),
]);
```

Expand All @@ -178,16 +217,16 @@ This is useful when you are defining a child theme that customizes some stuff bu
const config = {
default: {
color: 'white',
background: 'black'
background: 'black',
},
myTheme: {
color: 'purple',
background: 'green'
background: 'green',
},
myChildTheme: {
extends: 'myTheme',
background: 'red'
}
background: 'red',
},
};
```

Expand Down Expand Up @@ -267,8 +306,8 @@ The token `[local]` is also available which is the name of the original theme va
postcss([
require('postcss-themed')({
config,
modules: '[folder]-[name]-[local]'
})
modules: '[folder]-[name]-[local]',
}),
]);
```

Expand All @@ -280,8 +319,8 @@ To use the default function:
postcss([
require('postcss-themed')({
config,
modules: 'default'
})
modules: 'default',
}),
]);
```

Expand All @@ -293,11 +332,7 @@ const defaultLocalizeFunction = (
filePath: string,
css: string
) => {
const hash = crypto
.createHash('md5')
.update(css)
.digest('hex')
.slice(0, 6);
const hash = crypto.createHash('md5').update(css).digest('hex').slice(0, 6);
return `${filePath || 'default'}-${name}-${hash}`;
};
```
Expand All @@ -315,8 +350,8 @@ postcss([
.digest('hex')
.slice(0, 3);
return `${filePath}-${name}-${hash}`;
}
})
},
}),
]);
```

Expand All @@ -334,7 +369,7 @@ This feature is useful if your are converting your css-modules to typescript and

```js
postcss([
require('postcss-themed')({ config, forceEmptyThemeSelectors: true })
require('postcss-themed')({ config, forceEmptyThemeSelectors: true }),
]);
```

Expand All @@ -353,28 +388,28 @@ It is still recommended to set defaultTheme with this option, as any missing var
const config = {
default: {
light: {
color: 'purple'
color: 'purple',
},
dark: {
color: 'black'
}
color: 'black',
},
},
chair: {
light: {
color: 'beige'
color: 'beige',
},
dark: {
color: 'dark-purple'
}
}
color: 'dark-purple',
},
},
};

postcss([
require('postcss-themed')({
config,
defaultTheme: 'default',
forceSingleTheme: 'chair'
})
forceSingleTheme: 'chair',
}),
]);
```

Expand Down Expand Up @@ -416,20 +451,20 @@ If only a light theme is specified, this config option will just do in-place rep
```js
const config = {
default: {
color: 'purple'
color: 'purple',
},
chair: {
color: 'beige'
}
color: 'beige',
},
};

postcss([
require('postcss-themed')({
config,
defaultTheme: 'default',
forceSingleTheme: 'chair',
optimizeSingleTheme: true
})
optimizeSingleTheme: true,
}),
]);
```

Expand Down
Loading