styled-jsx-in-place
Version:
Full CSS support for JSX without compromises
42 lines (33 loc) • 1.11 kB
JavaScript
;
exports.__esModule = true;
exports["default"] = JSXStyle;
exports.flush = flush;
var _react = require("react");
var _stylesheetRegistry = _interopRequireDefault(require("./stylesheet-registry"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var styleSheetRegistry = new _stylesheetRegistry["default"]();
function JSXStyle(props) {
if (typeof window === 'undefined') {
styleSheetRegistry.add(props);
return null;
}
(0, _react.useLayoutEffect)(function () {
styleSheetRegistry.add(props);
return function () {
styleSheetRegistry.remove(props);
}; // props.children can be string[], will be striped since id is identical
}, [props.id, String(props.dynamic)]);
return null;
}
JSXStyle.dynamic = function (info) {
return info.map(function (tagInfo) {
var baseId = tagInfo[0];
var props = tagInfo[1];
return styleSheetRegistry.computeId(baseId, props);
}).join(' ');
};
function flush() {
var cssRules = styleSheetRegistry.cssRules();
styleSheetRegistry.flush();
return cssRules;
}