Skip to content

Instantly share code, notes, and snippets.

@HamedFathi
Last active April 23, 2020 08:43
Show Gist options
  • Save HamedFathi/fdfab96124eaff7f6099d360a1fcea4a to your computer and use it in GitHub Desktop.
Save HamedFathi/fdfab96124eaff7f6099d360a1fcea4a to your computer and use it in GitHub Desktop.
Au2EmotionAttr
<!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.ts.
-->
<body>
<my-app></my-app>
<script src="/dist/entry-bundle.js" data-main="main"></script>
</body>
</html>
{
"dependencies": {
"aurelia": "dev"
}
}
import { inject } from "aurelia";
import { css, cache } from 'emotion'
@inject(Element)
export class EmotionCustomAttribute {
constructor(private element: Element) {
}
afterAttach() {
if (this.isInShadow(this.element))
cache.sheet.container = this.element.getRootNode() as HTMLElement;
else
cache.sheet.container = document.head;
this.element.classList.add(css(this.value));
}
private isInShadow(element: Element): boolean {
return element.getRootNode() instanceof ShadowRoot;
}
}
import Aurelia from 'aurelia';
import { MyApp } from './my-app';
import { EmotionCustomAttribute } from './emotion-attr';
Aurelia.register(EmotionCustomAttribute).app(MyApp).start();
<!--
Try to create a paired css/scss/sass/less file like my-app.scss.
It will be automatically imported based on convention.
-->
<!--
There is no bundler config you can change in Dumber Gist to
turn on shadow DOM.
But you can turn shadow DOM on by adding a meta tag in every
html template:
<use-shadow-dom>
-->
<h1 class="message" emotion.bind="obj">${message}</h1>
export class MyApp {
public message: string = 'Hello Aurelia 2!';
private color = 'white';
public obj = {
backgroundColor: 'hotpink !important',
'&:hover': {
color: this.color
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment