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

Adding a default value inside the custom property statements #38

Merged
merged 6 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
170 changes: 157 additions & 13 deletions __tests__/modern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,45 @@ it('Creates a simple css variable based theme', () => {
);
});

it('inlineRootThemeVariables false', () => {
const config = {
default: {
color: 'purple',
extras: 'black'
},
mint: {
color: 'teal'
}
};

return run(
`
.test {
color: @theme color;
background-image: linear-gradient(to right, @theme color, @theme color)
}
`,
`
.test {
color: var(--color);
background-image: linear-gradient(to right, var(--color), var(--color))
}

:root {
--color: purple
}

.mint {
--color: teal
}
`,
{
config,
inlineRootThemeVariables: false
}
);
});

it('Creates a simple css variable based theme with light and dark', () => {
const config = {
default: {
Expand All @@ -52,6 +91,7 @@ it('Creates a simple css variable based theme with light and dark', () => {
color: 'black'
}
},

mint: {
color: 'teal'
},
Expand All @@ -77,7 +117,6 @@ it('Creates a simple css variable based theme with light and dark', () => {
color: var(--color);
background-image: linear-gradient(to right, var(--color), var(--color))
}

:root {
--color: purple
}
Expand Down Expand Up @@ -135,11 +174,7 @@ it('Produces a single theme', () => {
`,
`
.test {
color: var(--color);
}

:root {
--color: beige;
hipstersmoothie marked this conversation as resolved.
Show resolved Hide resolved
color: var(--color, beige);
}

.dark {
Expand Down Expand Up @@ -176,11 +211,7 @@ it('Produces a single theme with dark mode if default has it', () => {
`,
`
.test {
color: var(--color);
}

:root {
--color: teal;
color: var(--color, teal);
}

.dark {
Expand All @@ -204,6 +235,34 @@ it('Produces a single theme with variables by default', () => {
}
};

return run(
`
.test {
color: @theme color;
}
`,
`
.test {
color: var(--color, teal);
}
`,
{
config,
forceSingleTheme: 'mint'
}
);
});

it('Produces a single theme with variables by default with inlineRootThemeVariables off', () => {
const config = {
default: {
color: 'purple'
},
mint: {
color: 'teal'
}
};

return run(
`
.test {
Expand All @@ -221,11 +280,11 @@ it('Produces a single theme with variables by default', () => {
`,
{
config,
forceSingleTheme: 'mint'
forceSingleTheme: 'mint',
inlineRootThemeVariables: false
}
);
});

it('Optimizes single theme by removing variables', () => {
const config = {
default: {
Expand Down Expand Up @@ -449,3 +508,88 @@ it('Wrong key mentioned in theme configuration', () => {
}
);
});

it('With component Config', () => {
const config = {
default: {
light: {
background: 'purple',
extras: 'black'
},
dark: {
background: 'black'
}
},
mint: {
background: 'teal'
}
};

return run(
`
.test {
color: @theme background;
background-image: linear-gradient(to right, @theme background, @theme background)
}
`,
`
.test {
color: var(--background);
background-image: linear-gradient(to right, var(--background), var(--background))
}

:root {
--background: yellow
}

.dark {
--background: pink
}
.mint.light {
--background: teal
}
`,
{
config
},
'./__tests__/test-modern-themes-ts/test.css'
);
});
hipstersmoothie marked this conversation as resolved.
Show resolved Hide resolved

it('Some variables show inline and some show in root', () => {
const config = {
default: {
color: 'purple',
extras: 'black'
},
mint: {
color: 'teal'
}
};

return run(
`
.test {
color: @theme color;
background-image: linear-gradient(to right, @theme extras, @theme extras)
}
`,
`
.test {
color: var(--color, purple);
background-image: linear-gradient(to right, var(--extras), var(--extras))
}

:root {
--extras: black
}

.mint {
--color: teal
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

`,
{
config
}
);
});
27 changes: 6 additions & 21 deletions __tests__/precedence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ it('Overrides all themes from default', () => {
`,
`
.test {
color: var(--color);
color: var(--color, red);
}

:root {
--color: red;
}

.dark {
--color: blue;
Expand Down Expand Up @@ -74,12 +71,9 @@ it('Overrides dark themes from default', () => {
`,
`
.test {
color: var(--color);
color: var(--color, red);
}

:root {
--color: red;
}

.dark {
--color: blue;
Expand Down Expand Up @@ -125,13 +119,8 @@ it('Merges missing variables from single theme', () => {
`,
`
.test {
color: var(--color);
background-color: var(--bgColor);
}

:root {
--color: teal;
--bgColor: orange;
color: var(--color, teal);
background-color: var(--bgColor, orange);
}

.dark {
Expand Down Expand Up @@ -166,14 +155,10 @@ it('Merges single theme but leaves variables by default', () => {
`,
`
.test {
color: var(--color);
background-color: var(--bgColor);
color: var(--color, teal);
background-color: var(--bgColor, orange);
}

:root {
--color: teal;
--bgColor: orange;
}
`,
{
config,
Expand Down
11 changes: 11 additions & 0 deletions __tests__/test-modern-themes-ts/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default () => ({
default: {
light: {
background: 'yellow',
color: 'green'
},
dark: {
background: 'pink'
}
}
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"testPathIgnorePatterns": [
"/node_modules/",
"test-component-themes",
"test-modern-themes",
"test-utils"
],
"collectCoverageFrom": [
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import * as tsNode from 'ts-node';
import { getThemeFilename, normalizeTheme } from './common';
import { modernTheme } from './modern';
import { legacyTheme } from './legacy';
import { ComponentTheme, PostcssThemeConfig, PostcssThemeOptions, ThemeResolver } from './types';
import {
ComponentTheme,
PostcssThemeConfig,
PostcssThemeOptions,
ThemeResolver
} from './types';

const log = debug('postcss-themed');

Expand Down Expand Up @@ -47,7 +52,7 @@ const configForComponent = (
log(error);
return {};
}
}
};

/** Generate a theme */
const themeFile = (options: PostcssThemeOptions = {}) => (
Expand Down Expand Up @@ -77,7 +82,7 @@ const themeFile = (options: PostcssThemeOptions = {}) => (
const mergedConfig = merge(globalConfig, componentConfig);

if (caniuse.isSupported('css-variables', browserslist())) {
modernTheme(root, mergedConfig, globalConfig, options);
modernTheme(root, mergedConfig, options);
} else {
legacyTheme(root, mergedConfig, options);
}
Expand All @@ -86,12 +91,12 @@ const themeFile = (options: PostcssThemeOptions = {}) => (
root.source.processed = true;

if (!resolveTheme && root.source.input.file) {
const themeFilename = getThemeFilename(root.source.input.file)
const themeFilename = getThemeFilename(root.source.input.file);

if (fs.existsSync(themeFilename)) {
result.messages.push({
plugin: 'postcss-themed',
type: "dependency",
type: 'dependency',
file: themeFilename
});
}
Expand Down
Loading