-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 are not applied to react-select inside a shadow-DOM component #3680
Comments
I've received a suggestion from emotions library group to use a custom container. Could not achieve this. Any help on this is greatly appreciated. |
I have the same problem. The problem is related to "screen readers" feature. Also, that By the way, is there any way to deactivate this feature ("screen readers"), in order to not render this kind of dynamically |
@pusulurm did you manage to solve this any other way? |
@afvieira @pusulurm I have managed to solve this with a little workaround: import React from 'react';
import createCache from '@emotion/cache';
import { NonceProvider } from 'react-select';
import AsyncCreatableSelect from 'react-select/async-creatable';
class MyNonceProvider extends NonceProvider {
createEmotionCache = (nonce) => {
return createCache({ nonce, container: this.props.container });
};
}
const MySelect = () => {
return (
<MyNonceProvider container={shadowRootNode}>
<AsyncCreatableSelect
...
/>
</MyNonceProvider>
);
}; It would be nice to add this container parameter to |
same question here, I used it in Iframe, but when I click selector, it would add styles in parent window head. |
@afvieira , As I was in urgency , I ended up downgrading react select to 1.x version. This comes with a css file and I could bundle with my main css file. @norama Is there a way to avoid this flood of style tags from emotion ? Thanks again |
didnt work ontouch on mobile import React, { Component } from 'react';
import createCache from '@emotion/cache';
import Select, { NonceProvider } from 'react-select';
import memoizeOne from 'memoize-one';
class MyNonceProvider extends NonceProvider {
createEmotionCacheCustom = function (nonce) {
return createCache({ nonce, key: 'custom-select-style', container: this.props.container });
};
createEmotionCache = memoizeOne(this.createEmotionCacheCustom);
}
export default class CustomSelect extends Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
const shadowRootNode = document.getElementsByTagName('online-widget-osago')[0].shadowRoot.getElementById('osago-widget');
return (
<MyNonceProvider container={shadowRootNode}>
<Select
{...this.props}
/>
</MyNonceProvider>
);
}
}```
|
@JedWatson _this.onTouchEnd = function (event) {
if (_this.userIsDragging) return; // close the menu if the user taps outside
// we're checking on event.target here instead of event.currentTarget, because we want to assert information
// on events on child elements, not the document (which we've attached this handler to).
if ((_this.controlRef && !_this.controlRef.contains(event.target) && _this.menuListRef && !_this.menuListRef.contains(event.target)) &&
(_this.controlRef && !_this.controlRef.contains(event.composedPath()[0]) && _this.menuListRef && !_this.menuListRef.contains(event.composedPath()[0]))) {
_this.blurInput();
} // reset move vars
_this.initialTouchX = 0;
_this.initialTouchY = 0;
};` |
please update jet. thanks |
I'm having an issue where React-Select seems to not be working at all inside the shadow DOM. Focusing the element, onChange, nothing seems to be working properly. Everyone else's issue is just that the styles aren't being applied? So for anyone using React-Select inside a ShadowDOM, and having issues where events aren't firing: I was able to add the following indented lines from the code snippet below to where I attach my React component to the page and have events work again. This is being done from within a Chrome Extension, other browsers may behave differently. Object.defineProperty(reactRoot, 'ownerDocument', { value: shadowRoot });
shadowRoot.createElement = (...args) => document.createElement(...args);
shadowRoot.createElementNS = (...args) => document.createElementNS(...args);
shadowRoot.createTextNode = (...args) => document.createTextNode(...args); where |
@bonzoSPb |
In Select.js, check my fork |
Thank you @bonzoSPb ! 👍 It works great with a shadow DOM ! 💯 @JedWatson Can you please consider the solution of @bonzoSPb ? |
I'm running react-select in an iframe and getting the same styling bug as @afvieira where the screen reader gets displayed. I've tried running the code that @bonzoSPb and @norama have posted but the styling is still off. I tried console logging the nonce and seeing that it's returning null, could that be an issue?
|
@mikeyyyzhao Would you kindly mind creating a codesandbox demonstrating this issue? It would be much easier to get eyes on this and get this pushed up in priority. |
@ebonow sure thing, here's the codesandbox. As you can see, react-select is showing the screen reader text and style is incorrect. Thanks in advance for your help! |
I'll try to review this and get back to you soon. I'm still fairly new at this CSS-in-JS / Emotion thing, but I'll see what I can find out as I do know this is affecting others. |
@JedWatson First of all thank you for great librarary! Please consider on @bonzoSPb solution. There is two separate problems when yours library is used in Shadow DOM.
First problem can be easily fixed by adding custom NonceProvider and its not so annoying to fix it on users own. The second is much worse. It cannot be fixed without modification of library code and its lead to dirty hacks when you need to use it. Thanks for your interest in. |
This is an amazing library, but I am facing the same problem that @mikeyyyzhao described. I hope @ebonow finds a solution for that. |
Thanks for the ping @carlosribas . I'm currently working on a PR for customized aria-live messages. While working on this, it occurred to me that
Regarding @bonzoSPb , I agree that we should add a composedPath check, though we'd likely want to do a null guard check. One improvement upon this bit of code though, would be a check inside the entire Menu and not just the menuListRef should incase someone want to add a Header or Footer to their MenuList. There's an existing issue logged for this, so was hoping to take it on in one go. |
Sounds good. I'm really looking forward to this PR. Thank you @ebonow. |
Did you manage to stop the duplication flood? I'm facing the same issue when implementing @norama solution or even by creating my own NonceProvider from scratch. |
I had this issue with styles when some of my application components were inserted into a shadow root. I followed some of the suggestions here to simply use the emotion CacheProvider with a different container. import * as React from 'react';
import { CacheProvider } from '@emotion/core';
import createCache, { EmotionCache } from '@emotion/cache';
type Props = {
stylesRoot?: HTMLElement;
children: React.ReactNode;
};
export default function EmotionStyleSheetProvider(props: Props): React.ReactElement {
const [cache, setCache] = React.useState<EmotionCache | null>(null);
React.useEffect(() => {
setCache(createCache({ container: props.stylesRoot }));
}, [props.stylesRoot]);
return (
<React.Fragment>
{cache != null && <CacheProvider value={cache}>{props.children}</CacheProvider>}
</React.Fragment>
);
} In your application root, you could simply get the Hope this helps. |
@erkez solution works for us for a while.. but after this PR!2361 of Basically, the bug is that
Because of the bug, "The bug".. in this case, actually helping us... but it's now fixed, which mean we have to find another way. |
Any solution here? |
Not sure if you're using SSR or next.js but I was able to get the styles to inject in production by hiding a My solution is here: #3309 (comment) |
The behavior you are showing against a person you don't know and never met before is highly unacceptable. We have reported your abusive/harassing behavior. |
That being said: I tested around a bit and got an example working. The correct way to insert the styles into the shadow DOM is to use the // Create shadow and mounting point
class XSelect extends HTMLElement {
connectedCallback() {
this.somehowCreateShadowAndMountPoint();
const cache = createCache({
container: this.shadowRoot,
key: 'customElementCache'
});
ReactDOM.render(
<CacheProvider value={cache}>
<Select {...} />
</CacheProvider>,
this.mountPoint
);
}
} It is important that the |
Thanks for taking the time to take a look. I tried the demo and it does fix the issue of css not being inserted into the shadow DOM. But I can't seem to get it to work on mobile, when I open up this demo in Chrome DevTools in mobile mode, I can't select the dropdown at all. I'm guessing it's an interaction between mobile browsers and web components (unless I'm missing something simple) since I've been able to use this successfully with iframe versions of my embedded software. (Also, I apologize for negative comments the other day, I was not feeling well and took out frustrations among people who do not deserve that at all. I highly appreciate the work that goes into maintaining open source software and respect all the contributors. Thank you for responding to negativity with positiveness and spending time trying to help, that's very classy behavior.) |
Thanks for pointing this out, this worked for me as well! This patch allows me to use the select dropdown on mobile: #3680 (comment) |
I will close this issue now as the main problem has been addressed and a solution has been presented. |
Was there any chance to apply the changes to make it work on mobile? |
Solutions here are pretty vague and troublesome. IMHO styles should be applied to the react root element, not to the page head by default. |
IMHO it would be good that styles also should be provided in separated file, so people can add it manually where in their project |
react-select css forsvinner når man bruker shadowDom element. Dette fikser det. se f. eks JedWatson/react-select#3680 og emotion-js/emotion#3071 (comment) for mer info
react-select css forsvinner når man bruker shadowDom element. Dette fikser det. se f. eks JedWatson/react-select#3680 og emotion-js/emotion#3071 (comment) for mer info
So... won't fix and we'll have to add another dependency to our projects. Got it. |
From my understanding, we could use NonceProvider. We only need to add a |
I'm using react-select in my application and it is been working great until I hit this situation.
I have wrapped my react application as a shadow-DOM component to isolate styles from other applications. After this change react-select does not get any styles applied.
As per my analysis, react-select uses emotion library to build dynamic css and these styles are being added to document>head section. Since my application is a Shadow-DOM wrapped application, these external styles are not accessible to react-select.
I can see option to write my custom styles but that is very tidies to write all styles and upgrades will become unmanageable. Please suggest a solution for this case.
The text was updated successfully, but these errors were encountered: