Skip to content

Instantly share code, notes, and snippets.

@fkleuver
Forked from mattduffield/index.html
Last active October 11, 2020 22:11
Show Gist options
  • Select an option

  • Save fkleuver/d62b6a01124ab6760554d90d740e972f to your computer and use it in GitHub Desktop.

Select an option

Save fkleuver/d62b6a01124ab6760554d90d740e972f to your computer and use it in GitHub Desktop.
Aurelia theme engine
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dumber Gist</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<base href="/">
</head>
<!--
Dumber Gist uses dumber bundler, the default bundle file
is /dist/entry-bundle.js.
The starting module is pointed to "main" (data-main attribute on script)
which is your src/main.js.
-->
<body>
<my-app></my-app>
<script src="/dist/entry-bundle.js" data-main="main"></script>
</body>
</html>
{
"dependencies": {
"aurelia": "dev"
}
}
export default [
{
name: 'theme-gray',
variables: {
'--color-100': '#F7FAFC',
'--color-200': '#EDF2F7',
'--color-300': '#E2E8F0',
'--color-400': '#CBD5E0',
'--color-500': '#A0AEC0',
'--color-600': '#718096',
'--color-700': '#4A5568',
'--color-800': '#2D3748',
'--color-900': '#1A202C'
},
},
{
name: 'theme-red',
variables: {
'--color-100': '#FFF5F5',
'--color-200': '#FED7D7',
'--color-300': '#FEB2B2',
'--color-400': '#FC8181',
'--color-500': '#F56565',
'--color-600': '#E53E3E',
'--color-700': '#C53030',
'--color-800': '#9B2C2C',
'--color-900': '#742A2A'
},
},
{
name: 'theme-green',
variables: {
'--color-100': '#F0FFF4',
'--color-200': '#C6F6D5',
'--color-300': '#9AE6B4',
'--color-400': '#68D391',
'--color-500': '#48BB78',
'--color-600': '#38A169',
'--color-700': '#2F855A',
'--color-800': '#276749',
'--color-900': '#22543D'
},
},
{
name: 'theme-blue',
variables: {
'--color-100': '#EBF8FF',
'--color-200': '#BEE3F8',
'--color-300': '#90CDF4',
'--color-400': '#63B3ED',
'--color-500': '#4299E1',
'--color-600': '#3182CE',
'--color-700': '#2B6CB0',
'--color-800': '#2C5282',
'--color-900': '#2A4365'
},
},
];
:root {
/* Assume Gray Color Theme */
--color-100: #F7FAFC;
--color-200: #EDF2F7;
--color-300: #E2E8F0;
--color-400: #CBD5E0;
--color-500: #A0AEC0;
--color-600: #718096;
--color-700: #4A5568;
--color-800: #2D3748;
--color-900: #1A202C;
/* Color Theme reference */
--color-lightest: var(--color-100);
--color-lighter: var(--color-200);
--color-light: var(--color-300);
--color-normal-light: var(--color-400);
--color-normal: var(--color-500);
--color-normal-dark: var(--color-600);
--color-dark: var(--color-700);
--color-darker: var(--color-800);
--color-darkest: var(--color-900);
}
import Aurelia, { StyleConfiguration } from 'aurelia';
import { MyApp } from './my-app';
import main from './main.css';
Aurelia
.register(
StyleConfiguration.shadowDOM({ sharedStyles: [main] }),
)
.app(MyApp)
.start();
h1 {
color: var(--color-400);
}
<use-shadow-dom></use-shadow-dom>
<import from="./my-element"></import>
<h1>${message}</h1>
<my-element></my-element>
<select value.bind="theme"
change.trigger="theme.applyTo(style)"
style="margin-top: 10px;">
<option repeat.for="item of themes"
model.bind="item">
${item.name}
</option>
</select>
import {Theme} from './theme';
export class MyApp {
themes = [];
message = 'Hello Aurelia 2!';
style = document.documentElement.style;
async beforeBind() {
this.themes = (await import('./data')).default.map(Theme.fromJSON);
}
}
:host {
display: block;
background-color: var(--color-darker);
color: var(--color-lighter);
}
<use-shadow-dom></use-shadow-dom>
Hello World
export class MyElement {
}
export class Theme {
constructor(name, css) {
this.name = name;
this.css = css;
this.keys = Object.keys(css);
}
applyTo(style) {
for (const key of this.keys) {
style.setProperty(key, this.css[key]);
}
}
static fromJSON(json) {
return new Theme(json.name, json.variables);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment