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

Styles should work with ShadowDOM roots. #1517

Closed
xdumaine opened this issue Jan 3, 2020 · 3 comments
Closed

Styles should work with ShadowDOM roots. #1517

xdumaine opened this issue Jan 3, 2020 · 3 comments
Labels
has: pr Subject of a pull request

Comments

@xdumaine
Copy link

xdumaine commented Jan 3, 2020

Is your feature request related to a problem? Please describe.

When using styles inside a shadow root, styles are not applied, because react-native-web attaches styles to the document root

Describe a solution you'd like

Automatically attach styles to the shadow root instead of the document root if one is present. Resolve issues of cascading rules where a shadow root breaks up parents from children.

Describe alternatives you've considered

Avoid having styles compiled into a stylesheet with classes altogether, and have all styles inlined on components.

Additional context

The primary context here is that my react app is rendered inside a custom element, which can be included on anyone else's page as a widget. In order to get style isolation, I must render my app inside a shadow root (natively, or using react-shadow). This gives me proper style isolation from the outer (uncontrolled) application, but it also causes me to lose all of my styles from react-native-web.

@danperkins
Copy link

@necolas I was able to get a solution working where I add a style node as a child whenever mounting a new shadow DOM node. After that I added this functionality to createStyleResolver:

var sheets = {};
var savedRules = [];

// Call this when a new shadow DOM node is added passing in the reference to the style sheet
export function addSheet(id, sheet) {
  var orderedSheet = createOrderedCSSStyleSheet(sheet);
  sheets[id] = orderedSheet;
  savedRules.forEach((x) => orderedSheet.insert(x.style, x.group));
  return orderedSheet;
}

// Use this instead of sheet.insert
function insert(style, group) {
  Object.values(sheets).forEach(function(sheet) {
    sheet.insert(style,group);
  });
  savedRules.push({style: style, group: group});
}

I saw that NativeMethodsMixin has an approach for providing a custom style resolver when resolving styles but I couldn't figure out a way to add a custom resolver (or override the default resolver) in the normal createElement flow. If there is an easy way to do this could you point me to it? If not and you have an idea about how the infrastructure could be extended to support this let me know and I would be happy to submit a PR

@xdumaine
Copy link
Author

@necolas any chance you could give us some brief thoughts or guidance on this one?

@necolas
Copy link
Owner

necolas commented Jan 31, 2022

See #2208 for a build that should support ShadowDOM use

@necolas necolas added this to the 0.18: StyleSheet milestone Mar 1, 2022
necolas added a commit that referenced this issue Mar 2, 2022
* Improves React Native compatibility by making StyleSheet.create the identify function.
* Improves React 18 support by inserting styles on eval.
* Supports use with multiple windows (i.e., iframes) and shadow roots.
* Supports nested LTR/RTL layouts.
* Supports 3rd party compilers and extraction to static CSS.
* Fixes static and dynamic short/longform deduplication.
* Reduces browser support: Safari 10.1+, Chromium Edge, no IE, no legacy Android browsers.
* Removes automatic vendor-prefixing of inline styles (for better perf).
* Removes focus-visible polyfill as modern browsers no longer show focus rings for mouse interactions.

Close #2208
Fix #2138
Fix #2196
Fix #2007
Fix #1517
@necolas necolas added the has: pr Subject of a pull request label Mar 2, 2022
rnike pushed a commit to VeryBuy/react-native-web that referenced this issue Sep 13, 2022
* Improves React Native compatibility by making StyleSheet.create the identify function.
* Improves React 18 support by inserting styles on eval.
* Supports use with multiple windows (i.e., iframes) and shadow roots.
* Supports nested LTR/RTL layouts.
* Supports 3rd party compilers and extraction to static CSS.
* Fixes static and dynamic short/longform deduplication.
* Reduces browser support: Safari 10.1+, Chromium Edge, no IE, no legacy Android browsers.
* Removes automatic vendor-prefixing of inline styles (for better perf).
* Removes focus-visible polyfill as modern browsers no longer show focus rings for mouse interactions.

Close necolas#2208
Fix necolas#2138
Fix necolas#2196
Fix necolas#2007
Fix necolas#1517
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has: pr Subject of a pull request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants