@onesy/style
Version:
CSS in JS styling solution
65 lines (64 loc) • 2.79 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const merge_1 = __importDefault(require("@onesy/utils/merge"));
const try_1 = __importDefault(require("@onesy/utils/try"));
const OnesyStyle_1 = __importDefault(require("./OnesyStyle"));
const OnesyStyleSheetManager_1 = __importDefault(require("./OnesyStyleSheetManager"));
const OnesyTheme_1 = __importDefault(require("./OnesyTheme"));
const utils_1 = require("./utils");
const optionsDefault = {
mode: 'regular',
onesy_style: {
get: OnesyStyle_1.default.first.bind(OnesyStyle_1.default),
},
onesy_theme: {
get: OnesyTheme_1.default.first.bind(OnesyTheme_1.default),
}
};
function style(value_, options_ = {}) {
const options = (0, merge_1.default)(options_, optionsDefault, { copy: true });
// Onesy style
let onesyStyle = options.onesy_style.value || ((0, utils_1.is)('function', options.onesy_style.get) && options.onesy_style.get(options.element));
if (onesyStyle === undefined)
onesyStyle = new OnesyStyle_1.default();
// Onesy theme
const onesyTheme = options.onesy_theme.value || ((0, utils_1.is)('function', options.onesy_theme.get) && options.onesy_theme.get(options.element));
// Make value if it's a function
const value = (0, utils_1.is)('function', value_) ? (0, try_1.default)(() => value_(onesyTheme)) : value_;
// Make an instance of onesyStyleSheetManager
const onesyStyleSheetManager = new OnesyStyleSheetManager_1.default(value, {
mode: options.mode,
pure: false,
priority: 'upper',
onesyTheme,
onesyStyle,
name: options.name,
style: {
attributes: {
method: 'style'
}
}
});
const response = {
ids: onesyStyleSheetManager.ids,
onesy_style_sheet_manager: onesyStyleSheetManager,
sheets: onesyStyleSheetManager.sheets,
add: onesyStyleSheetManager.add.bind(onesyStyleSheetManager),
set props(value__) { onesyStyleSheetManager.props = value__; },
update: onesyStyleSheetManager.update.bind(onesyStyleSheetManager),
remove: onesyStyleSheetManager.remove.bind(onesyStyleSheetManager),
addRule: onesyStyleSheetManager.sheets.static[0] && onesyStyleSheetManager.sheets.static[0].addRule.bind(onesyStyleSheetManager.sheets.static[0]),
};
// if add
if (options.add) {
const addResponse = response.add();
// return
return (options.return ? (addResponse[options.return] || addResponse) : addResponse);
}
// Response
return response;
}
exports.default = style;