question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for web-components

See original GitHub issue

This is all @michael-klein’s initiative 😄to add support for web-components with a parametrized target for styled.

@michael-klein feel free to add more details. I’ve created this, to just put it on the roadmap 💯

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
michael-kleincommented, Feb 25, 2019

Hey there 😃

So, for web-components, a single global style sheet obviously doesn’t work, due to shadow DOM. Therefore, goober would need a mechanism to render to different targets (like the shadowRoot of a web component).

I played with a few ways to implement this and my current favorite is to do it by means of a function binding on styled (which would pass the target to the functions that need it). This change would also incorporate setting the pragma this way and do away with setPragma (htm is the inspiration for this idea).

Basically, users would bind like this to change the target and/or pragam:

styled = styled.bind({
     h: somePragma,
     target: this.shadowRoot // in case of a web-component
})

I have this change already implemented in a branch and it actually shaves off some bytes from the bundle size. The implementation is mostly in styled.js:

export const styled = function(tag) {
  let context = this || {};
  const h = context.h;
  let target = context.target || (document && document.head);
  return (str, ...defs) => props => {
    const className = getClassNameForCss(getCss(str, defs, props), target);

    // To be used for 'vanilla'
    if (!h || !tag) return className;

    return h(
      tag,
      Object.assign({}, props, {
        className:
          (props && props.className ? props.className + " " : "") + className
      })
    );
  };
};

and sheet.js

export const add = (hash, css, target) => {
  // If this is already present just stop
  if (styles[hash] == css) {
    return;
  }

  // Keep the hash and the value in _cache_
  styles[hash] = css;

  // If we're no the client
  if (target) {
    let sheet = target.querySelector("style[" + SHEET_ID + "]");
    if (!sheet) {
      sheet = document.createElement("style");
      sheet.setAttribute(SHEET_ID, "");
      target.appendChild(sheet);
    }

    if (!sheet.firstChild) {
      sheet.appendChild(document.createTextNode(""));
    }

    // Append the css into the style sheet
    sheet.firstChild.data += css;
  }
};
1reaction
geocinecommented, Mar 10, 2019

@michael-klein is there a documentation on how to use this? Specifically using the vanilla css

Read more comments on GitHub >

github_iconTop Results From Across the Web

"web components" | Can I use... Support tables for ... - CanIUse
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
Web Components - MDN Web Docs - Mozilla
Web Components is a suite of different technologies allowing you to create ... Add child elements, event listeners, etc., to the shadow DOM ......
Read more >
webcomponents.org
Browser support · Chrome · Opera · Safari · Firefox · Edge ...
Read more >
Custom Elements Everywhere
Custom Elements are the lynchpin in the Web Components specifications. They give developers the ability to ... feat: Add native support for custom...
Read more >
A Complete Introduction to Web Components in 2022 - Kinsta
Web Components are a standard way to create reusable and modular HTML elements without using a JavaScript framework.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found