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

Add register.style for registering global component styles #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import './mods/00-title.js'
import './mods/01-meta.js'
import './mods/02-hooks.js'
import './mods/03-transforms.js'
import './mods/05-template.js'
import './mods/04-template.js'
import './mods/07-query.js'
import './mods/08-style.js'
import './mods/10-script.js'
Expand Down
2 changes: 1 addition & 1 deletion src/mods/05-template.js → src/mods/04-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { define } from '../define.js'
import { warn } from '../help.js' //


define.register(5, 'template', (context, [args]) => {
define.register(4, 'template', (context, [args]) => {
if(!args){
return {
constructor: function(meta){
Expand Down
19 changes: 18 additions & 1 deletion src/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ register.auto = finder => {

// We sneakily define another mod here, to define custom elements in the
// template
define.register(6, Symbol(), context => {
define.register(5, Symbol(), context => {
if(context.__template) from(context.__template)
return {}
})
Expand All @@ -79,3 +79,20 @@ register.auto = finder => {
from(document)
}
}

register.style = url => {
const sheet = new CSSStyleSheet
define.register(6, Symbol(), context => {
return {
connectedCallback: function(meta){
const root = meta.root.mode ? meta.root : this.getRootNode()
const sheets = root.adoptedStyleSheets
if(sheets.includes(sheet)) return
root.adoptedStyleSheets = [...sheets, sheet]
}
}
})
return fetch(url)
.then(response => response.text())
.then(styles => sheet.replace(styles))
}