@morfeo/jss
Version:

39 lines • 1.54 kB
JavaScript
import { morfeo } from '@morfeo/web';
import { deepMerge } from '@morfeo/utils';
import jss from './initJSS';
import { MorfeoSheetsRegistry } from './registry';
export function getStyleSheet(styles, options) {
const sheet = jss.createStyleSheet(styles, options);
MorfeoSheetsRegistry.add(sheet);
return sheet;
}
export function getStyles(styles, options) {
let sheet = getStyleSheet(styles, options);
sheet.attach();
let classes = sheet.classes;
let currentStyles = Object.assign({}, styles);
function onThemeChange() {
sheet.detach();
sheet = getStyleSheet(currentStyles, Object.assign(Object.assign({}, options), { generateId: ({ key }) => classes[key] }));
sheet.attach();
}
const update = (props) => {
currentStyles = deepMerge(currentStyles, props);
sheet.addRules(currentStyles, Object.assign(Object.assign({}, options), { generateId: rule => {
// We need to ignore this line because the JSS's RuleOptions interface is (apparently) wrong
// and it doesn't expose the attribute name.
// @ts-ignore
const ruleName = rule.options.name;
return classes[ruleName];
} }));
classes = sheet.classes;
return classes;
};
const unsubscribe = morfeo.theme.subscribe(onThemeChange);
const destroy = () => {
sheet.detach();
unsubscribe();
};
return { classes, sheet, jss, destroy, update };
}
//# sourceMappingURL=getStyles.js.map