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.

Scoped css with pure css string

See original GitHub issue

I tried to add scoped CSS plug-in to preact. But I have a problem. Can i get the component instance in “vnode hook” function?

import { options } from 'preact'

options.vnode = (vnode, component) =>{
  //generate scoped attr by component

  //add attr to vnode
}

Thanks! -dntzhang

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
developitcommented, Oct 1, 2019

@dntzhang technically there is a reference to the component instance associated with a given VNode, as a ._component property (compressed in production as .__c). However, VNodes are created long before they can be associated with a component, so this is not a reliable method of associating vnodes and components during the options.vnode hook.

The workaround is to store the component’s associated VNode, then reference it dynamically in your options.vnode hook:

import { options } from 'preact';

let componentNode;

// store a reference to the "current component" vnode
let oldDiff = options._diff || options.__b;
options._diff = options.__b = vnode => {
  componentNode = vnode;
  if (oldDiff) oldDiff(vnode);
};

// reset component reference at end of diffing:
let oldDiffed = options.diffed;
options.diffed = vnode => {
  if (componentNode === vnode) componentNode = null;
  if (oldDiffed) oldDiffed(vnode);
};

// our vnode hook looks up the associated component
let old = options.vnode;
options.vnode = vnode => {
  const component = componentNode && (componentNode._component || componentNode.__c);
  if (component) {
    // component is the component instance
    component.css;
    // example: assign component's unique CSS ID:
    (vnode.props || (vnode.props = {}))[component._styleId] = '';
  }
  if (old) old(vnode);
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Scoped CSS / Sylvain Lesage - Observable
Use scoped() to wrap the HTML content and CSS styles. It creates a <div> wrapper with a unique class name ( scoped-dom-1671408000226-9578430986 ) ......
Read more >
Saving the Day with Scoped CSS
One little known feature of HTML5 is Scoped CSS. It's an attribute for style blocks that may change the way we tackle certain...
Read more >
scope - CSS: Cascading Style Sheets - MDN Web Docs
The :scope CSS pseudo-class represents elements that are a reference point for selectors to match against.
Read more >
How to import css file as local scoped in ReactJS?
I want the home.css file to work only with the home.js view. Home.css .text1 { font-size: 1em ...
Read more >
SFC CSS Features - Vue.js
Scoped styles do not eliminate the need for classes. Due to the way browsers render various CSS selectors, p { color: red }...
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