material-web icon indicating copy to clipboard operation
material-web copied to clipboard

MWC Snackbar: CSS variable for background color

Open maicol07 opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe.
Can't change the background color of a snackbar.
For example, the standard snackbar is great for dark themes, but not on light ones

Describe the solution you'd like
Support snackbar theming (css variable for background color)

Describe alternatives you've considered
It's possible to do this only with JS at the moment

maicol07 avatar Feb 12 '21 07:02 maicol07

Thanks for the issue!

dfreedm avatar Feb 13 '21 00:02 dfreedm

@maicol07 out of curiosity: how are you handling it with JS?

veller avatar Dec 27 '21 18:12 veller

@veller at the moment you have only two possibilities:

  • Extending the webcomponent and then add the background color (recommended):
import {Snackbar as MWCSnackbar} from '@material/mwc-snackbar`;
import {css} from 'lit';

class Snackbar extends MWCSnackbar {
	static styles = [MWCSnackbar.styles, css`.mdc-snackbar__surface { background-color: white }`]
}
window.customElements.define('snack-bar', Snackbar);
// ^^ You can choose any element tag you like
  • Traverse the shadow root to change the color manually (might not work):
document.getElementsByTagName('mwc-snackbar')[0].shadowRoot.firstElementChild.firstElementChild.style['background-color'] = 'white'

maicol07 avatar Dec 27 '21 19:12 maicol07