Style Isolation
CSS selectors are globally scoped, which can easily cause conflicts. To prevent the styles of your custom visualization components from affecting other Quick BI pages, use one of the following methods to isolate styles.
Shadow DOM (recommended)
Shadow DOM encapsulates markup, styles, and behavior, isolating them from the rest of the page to prevent interference. In Quick BI, custom visualization components render inside a Shadow DOM node by default. If you do not need Shadow DOM, you can disable it in the custom visualization management platform.
Shadow DOM requires a browser equivalent to or later than Internet Explorer 11. For more information, see caniuse.
CSS Modules
CSS Modules let you import CSS files like JavaScript modules. Each class name becomes a property of the imported object and receives a unique hash at compile time.
If your custom visualization component must support older browsers, use CSS Modules for style isolation. The seed project has built-in support for CSS Modules. Any file ending with .module.css can be imported and used directly.
// index.module.css
.test-class {
color: #ddd;
}
// Compiled index.module.css
._2GdXV4wBykbQ3MN-f1VJv7 {
color: #ddd;
}
import styles from './index.css';
// Use the class
document.getElementById('foo').classList.add(styles['test-class'])
BEM Naming Convention
If you do not use CSS Modules, make sure your CSS class names are unique.
CSS lacks true namespaces. To simulate namespace-like behavior, follow the CSS BEM naming convention.
// Good: define all styles within the .my-component parent class.
.my-component {
color: #ddd;
ul {
color: #aaa
}
&__header {}
&__header--title {}
}
// Bad: avoid defining styles outside the parent class.
ul {
background: #aaa
}