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

A conflict between the styles of the extension's modal window and the page's styles #90

Open
kpripper opened this issue Aug 20, 2024 · 2 comments

Comments

@kpripper
Copy link

Greetings! My extension has modal windows that are added by script to the page.

There is a conflict of styles - either the page styles affect the display of elements in the modal window, or vice versa.

What are the solutions?

I can write very specific classes or even inline styles in a modal window, but I don't like that option.

@sakibul-islam
Copy link
Contributor

Use Shadow DOM

Shadow DOM encapsulates the modal's styles, preventing them from leaking out and being affected by the page's styles. This ensures that the modal's styling remains isolated.

You can create a shadow root for your modal element like this:

const modalElement = document.createElement('div');
const shadowRoot = modalElement.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `
    <style>
        /* Your modal styles go here */
    </style>
    <div class="modal-content">
        <!-- Modal content -->
    </div>
`;
document.body.appendChild(modalElement);

@kpripper
Copy link
Author

Thanks! Or like this

import styles from './App.css'

  React.useEffect(() => {
    const styleElement = document.createElement('style')
    styleElement.textContent = styles
    document.querySelector('#app-root').shadowRoot.appendChild(styleElement)
  }, [])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants