I have this in my global script, similar to ion, but what I was doing was setting it on every element. Which technically worked, but noticing some odd issues with nesting and react handling it. Which would make sense. I’ll give this a shot, the purpose is to test out new designs on select components.
import { setMode, getMode } from '@stencil/core';
import { Mode } from '../utils/interfaces';
let defaultMode: Mode;
export const getBrand = (ref?: any): Mode => {
return (ref && getMode(ref)) || defaultMode;
};
const isAllowedBrand = (elmMode: string) => ['default', 'next'].includes(elmMode);
const isWebComponentElement = (elm: any) => elm.tagName && elm.tagName.startsWith('XDS-');
export default () => {
setMode((elm: any) => {
while (elm) {
const elmMode = (elm as any).mode || elm.getAttribute('mode');
if (elmMode) {
if (isAllowedBrand(elmMode)) {
return elmMode;
} else if (isWebComponentElement(elm)) {
console.warn('Invalid mode: "' + elmMode + '", expected: "default" or "next"');
}
}
elm = elm.parentElement;
}
return 'default';
});
};