oncss 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core.js +1 -1
- package/core.js.map +2 -2
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/readme.md +397 -398
- package/types.d.ts +5 -4
package/core.js
CHANGED
|
@@ -186,7 +186,7 @@ const style = (_css, cls, opt) => {
|
|
|
186
186
|
if (opt?.getValue) {
|
|
187
187
|
val = opt.getValue(prop, val, _css);
|
|
188
188
|
} else if (opt?.aliases && opt.aliases[prop]) {
|
|
189
|
-
let _props = opt.aliases[prop](
|
|
189
|
+
let _props = opt.aliases[prop](val);
|
|
190
190
|
if (_props) {
|
|
191
191
|
let r = style(_props, classname, opt);
|
|
192
192
|
r.stack = r.stack.replace(`${classname}{`, "").replace(`}`, "");
|
package/core.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/core.ts"],
|
|
4
|
-
"sourcesContent": ["import { CSSOptionProps, CSSFactoryType, CSSProps } from './types';\r\n\r\nconst _global: any = typeof window !== 'undefined' ? window : global;\r\n_global.Factory = _global.Factory || new Map<string, CSSFactoryType>();\r\nexport const CSSFactory = _global.Factory as Map<string, CSSFactoryType>\r\n\r\nexport const uid = (str: string) => {\r\n var hash = 0, len = str.length;\r\n for (var i = 0; i < len; i++) {\r\n hash = ((hash << 5) - hash) + str.charCodeAt(i);\r\n hash |= 0;\r\n }\r\n let id = hash.toString(32).slice(-10).replace(/-/g, \"\");\r\n if (/^\\d/.test(id.charAt(0))) id = 'c' + id;\r\n return id;\r\n}\r\n\r\nconst number_val_props = [\r\n \"fontWeight\",\r\n \"font-weight\",\r\n \"lineHeight\",\r\n \"line-height\",\r\n \"opacity\",\r\n \"zIndex\",\r\n \"z-index\",\r\n \"flex\",\r\n \"order\",\r\n \"flexGrow\",\r\n \"flex-grow\",\r\n \"flexShrink\",\r\n \"flex-shrink\",\r\n \"flexBasis\",\r\n \"flex-basis\",\r\n \"columns\",\r\n \"perspective\",\r\n \"stroke-dashoffset\"\r\n]\r\n\r\nexport const formatCSSProp = (prop: string) => prop.split(/(?=[A-Z])/).join(\"-\").toLowerCase();\r\nexport const formatCSSValue = (prop: string, val: any) => typeof val === 'number' && !number_val_props.includes(prop) ? `${val}px` : val\r\n\r\nconst PREFIXES = ['webkit', 'moz', 'ms', 'o'];\r\nlet _declaration: CSSStyleDeclaration;\r\nconst PREFIXCACHE = new Map();\r\n\r\nexport const cssPrefix = (prop: string, value: string): { prop: string, value: string } => {\r\n value = formatCSSValue(prop, value);\r\n prop = formatCSSProp(prop);\r\n\r\n if (typeof window === 'undefined') {\r\n return { prop, value };\r\n }\r\n\r\n const declaration = _declaration || (_declaration = document.createElement(\"div\").style);\r\n value = value?.toString();\r\n\r\n // Check if the property and value work as is\r\n if (declaration.setProperty(prop, value), declaration.getPropertyValue(prop) === value) {\r\n return { prop, value };\r\n }\r\n\r\n // Check cached property and value prefix\r\n const cached = PREFIXCACHE.get(prop);\r\n if (cached) {\r\n return { prop: cached._prop, value: `${cached._vprefix}${value}` };\r\n }\r\n\r\n let _prop = prop;\r\n let _value = value;\r\n let _vprefix = '';\r\n\r\n // Try property prefixes\r\n const camelCaseProp = prop.includes('-') ? prop.replace(/-([a-z])/g, (_, c) => c.toUpperCase()) : prop;\r\n for (const prefix of PREFIXES) {\r\n if (declaration[`${prefix}${camelCaseProp}` as any] !== undefined) {\r\n _prop = `-${prefix}-${prop}`;\r\n break;\r\n }\r\n }\r\n\r\n // Check if prefixed property works with the value\r\n declaration.setProperty(_prop, value);\r\n if (!declaration.getPropertyValue(_prop)) {\r\n for (const prefix of PREFIXES) {\r\n const prefixedValue = `-${prefix}-${value}`;\r\n if (declaration.setProperty(_prop, prefixedValue), declaration.getPropertyValue(_prop) === prefixedValue) {\r\n _value = prefixedValue;\r\n _vprefix = `-${prefix}-`;\r\n break;\r\n }\r\n }\r\n }\r\n\r\n PREFIXCACHE.set(prop, { _prop, _vprefix });\r\n return { prop: _prop, value: _value };\r\n};\r\n\r\nexport const style = <Aliases, BreakpointKeys extends string>(_css: CSSProps<Aliases, BreakpointKeys>, cls?: string, opt?: CSSOptionProps<Aliases, BreakpointKeys>) => {\r\n let cachekey\r\n let classname = cls\r\n if (!cls) {\r\n cachekey = JSON.stringify(_css)\r\n const has = CSSFactory.get(cachekey)\r\n if (has) {\r\n has.cache = true\r\n return has\r\n }\r\n classname = (opt?.classPrefix || \"\") + uid(cachekey)\r\n } else if (typeof cls !== 'string') {\r\n throw new Error(`Invalid class name: ${cls}`)\r\n }\r\n\r\n let stack: any = [`${classname}{`]\r\n let medias: any = {}\r\n let skiped: any = {}\r\n\r\n for (let prop in _css) {\r\n let val = (_css as any)[prop]\r\n let firstChar = prop.charAt(0)\r\n if (firstChar === '&') {\r\n let ncls = prop.replace(/&/g, classname as string)\r\n const r: any = style(val, ncls, opt)\r\n if (opt?.skipProps) {\r\n skiped = {\r\n ...skiped,\r\n ...r.skiped\r\n }\r\n }\r\n stack.push(r.stack)\r\n } else if (firstChar === '@') {\r\n if (prop.startsWith(\"@global\") || prop.startsWith(\"@keyframes\")) {\r\n let _css = ''\r\n for (let selector in val) {\r\n let r: any = style(val[selector], selector, opt)\r\n _css += r.stack\r\n if (opt?.skipProps) {\r\n skiped = {\r\n ...skiped,\r\n ...r.skiped\r\n }\r\n }\r\n }\r\n if (prop.startsWith(\"@keyframes\")) {\r\n stack.push(`${prop}{${_css}}`)\r\n } else {\r\n stack.push(_css)\r\n }\r\n } else {\r\n let r: any = style(val, classname, opt)\r\n const atcss = prop + \"{\" + r.stack + \"}\"\r\n stack.push(atcss)\r\n if (opt?.skipProps) {\r\n skiped = {\r\n ...skiped,\r\n ...r.skiped\r\n }\r\n }\r\n }\r\n } else if (typeof val === 'object') {\r\n for (let media in val) {\r\n if (typeof val[media] === 'object' || typeof val[media] === 'function' || Array.isArray(val[media])) {\r\n throw new Error(`Invalid css value: ${val[media]}`);\r\n }\r\n let breakpoint = media\r\n let isNumber = !isNaN(parseInt(breakpoint))\r\n if (!isNumber) {\r\n if (opt?.breakpoints && !isNaN(parseInt((opt.breakpoints as any)[media]))) {\r\n breakpoint = opt.breakpoints[media as BreakpointKeys].toString()\r\n } else {\r\n throw new Error(`Invalid breakpoint prop: ${media}`);\r\n }\r\n }\r\n let _css = { [prop]: val[media] }\r\n let r: any = style(_css, classname, opt)\r\n let _style = r.stack\r\n let mediakey = `@media (min-width: ${breakpoint}px)`\r\n medias[mediakey] = medias[mediakey] ? medias[mediakey] + _style : _style\r\n if (opt?.skipProps) {\r\n skiped = {\r\n ...skiped,\r\n ...r.skiped\r\n }\r\n }\r\n }\r\n } else {\r\n if (opt?.skipProps && opt.skipProps(prop, val)) {\r\n if (!skiped[classname as string]) skiped[classname as string] = []\r\n skiped[classname as string].push(prop)\r\n continue\r\n }\r\n if (opt?.getProps) {\r\n let _props: any = opt.getProps(prop, val, _css)\r\n if (_props) {\r\n let r: any = style(_props, classname, {\r\n ...opt,\r\n getProps: undefined\r\n })\r\n if (opt?.skipProps) {\r\n skiped = {\r\n ...skiped,\r\n ...r.skiped\r\n }\r\n }\r\n stack.push(r.stack)\r\n continue;\r\n }\r\n }\r\n if (opt?.getValue) {\r\n val = opt.getValue(prop, val, _css)\r\n } else if (opt?.aliases && (opt.aliases as any)[prop]) {\r\n let _props = (opt.aliases as any)[prop](prop, val)\r\n if (_props) {\r\n let r: any = style(_props, classname, opt)\r\n r.stack = r.stack.replace(`${classname}{`, '').replace(`}`, '')\r\n stack[0] += r.stack\r\n skiped[classname as string] = r.skiped\r\n continue;\r\n }\r\n }\r\n let p = cssPrefix(prop, val)\r\n stack[0] += `${p.prop}:${p.value};`\r\n }\r\n }\r\n stack[0] += \"}\"\r\n if (stack[0] === `${classname}{}`) {\r\n stack[0] = \"\"\r\n }\r\n stack = stack.join('')\r\n for (let media in medias) {\r\n stack += `${media}{${medias[media].replace(new RegExp(`}\\\\${classname}\\\\{`, 'g'), '')}}`\r\n }\r\n\r\n if (cachekey) {\r\n stack = stack.replace(new RegExp(classname as string, 'g'), `.${classname}`)\r\n const r = {\r\n cache: false,\r\n cachekey,\r\n classname: classname as string,\r\n css: stack,\r\n cssraw: _css,\r\n skiped,\r\n getStyleTag: () => document?.querySelector(`[data-oncss=\"${classname}\"]`) as HTMLStyleElement | null,\r\n deleteStyle: () => {\r\n const tag = document?.querySelector(`[data-oncss=\"${classname}\"]`)\r\n tag && tag.remove()\r\n },\r\n }\r\n r.toString = () => classname as string\r\n CSSFactory.set(cachekey, r)\r\n let inject = opt?.injectStyle || true\r\n if (inject && typeof window !== 'undefined') {\r\n if (!document.querySelector(`[data-oncss=\"${classname}\"]`)) {\r\n const tag = document.createElement(\"style\");\r\n tag.innerHTML = r.css\r\n tag.setAttribute(`data-oncss`, classname as string)\r\n document.head.append(tag)\r\n }\r\n }\r\n return r\r\n }\r\n return { stack, skiped }\r\n}"],
|
|
5
|
-
"mappings": "AAEA,MAAM,UAAe,OAAO,WAAW,cAAc,SAAS;AAC9D,QAAQ,UAAU,QAAQ,WAAW,oBAAI,IAA4B;AAC9D,MAAM,aAAa,QAAQ;AAE3B,MAAM,MAAM,CAAC,QAAgB;AAChC,MAAI,OAAO,GAAG,MAAM,IAAI;AACxB,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC1B,YAAS,QAAQ,KAAK,OAAQ,IAAI,WAAW,CAAC;AAC9C,YAAQ;AAAA,EACZ;AACA,MAAI,KAAK,KAAK,SAAS,EAAE,EAAE,MAAM,GAAG,EAAE,QAAQ,MAAM,EAAE;AACtD,MAAI,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,EAAG,MAAK,MAAM;AACzC,SAAO;AACX;AAEA,MAAM,mBAAmB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AA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,IAAS,MAAMA,OAAM,WAAW,GAAG;AACvC,YAAI,SAAS,EAAE;AACf,YAAI,WAAW,sBAAsB,UAAU;AAC/C,eAAO,QAAQ,IAAI,OAAO,QAAQ,IAAI,OAAO,QAAQ,IAAI,SAAS;AAClE,YAAI,KAAK,WAAW;AAChB,mBAAS;AAAA,YACL,GAAG;AAAA,YACH,GAAG,EAAE;AAAA,UACT;AAAA,QACJ;AAAA,MACJ;AAAA,IACJ,OAAO;AACH,UAAI,KAAK,aAAa,IAAI,UAAU,MAAM,GAAG,GAAG;AAC5C,YAAI,CAAC,OAAO,SAAmB,EAAG,QAAO,SAAmB,IAAI,CAAC;AACjE,eAAO,SAAmB,EAAE,KAAK,IAAI;AACrC;AAAA,MACJ;AACA,UAAI,KAAK,UAAU;AACf,YAAI,SAAc,IAAI,SAAS,MAAM,KAAK,IAAI;AAC9C,YAAI,QAAQ;AACR,cAAI,IAAS,MAAM,QAAQ,WAAW;AAAA,YAClC,GAAG;AAAA,YACH,UAAU;AAAA,UACd,CAAC;AACD,cAAI,KAAK,WAAW;AAChB,qBAAS;AAAA,cACL,GAAG;AAAA,cACH,GAAG,EAAE;AAAA,YACT;AAAA,UACJ;AACA,gBAAM,KAAK,EAAE,KAAK;AAClB;AAAA,QACJ;AAAA,MACJ;AACA,UAAI,KAAK,UAAU;AACf,cAAM,IAAI,SAAS,MAAM,KAAK,IAAI;AAAA,MACtC,WAAW,KAAK,WAAY,IAAI,QAAgB,IAAI,GAAG;AACnD,YAAI,SAAU,IAAI,QAAgB,IAAI,EAAE,
|
|
4
|
+
"sourcesContent": ["import { CSSOptionProps, CSSFactoryType, CSSProps } from './types';\n\nconst _global: any = typeof window !== 'undefined' ? window : global;\n_global.Factory = _global.Factory || new Map<string, CSSFactoryType>();\nexport const CSSFactory = _global.Factory as Map<string, CSSFactoryType>\n\nexport const uid = (str: string) => {\n var hash = 0, len = str.length;\n for (var i = 0; i < len; i++) {\n hash = ((hash << 5) - hash) + str.charCodeAt(i);\n hash |= 0;\n }\n let id = hash.toString(32).slice(-10).replace(/-/g, \"\");\n if (/^\\d/.test(id.charAt(0))) id = 'c' + id;\n return id;\n}\n\nconst number_val_props = [\n \"fontWeight\",\n \"font-weight\",\n \"lineHeight\",\n \"line-height\",\n \"opacity\",\n \"zIndex\",\n \"z-index\",\n \"flex\",\n \"order\",\n \"flexGrow\",\n \"flex-grow\",\n \"flexShrink\",\n \"flex-shrink\",\n \"flexBasis\",\n \"flex-basis\",\n \"columns\",\n \"perspective\",\n \"stroke-dashoffset\"\n]\n\nexport const formatCSSProp = (prop: string) => prop.split(/(?=[A-Z])/).join(\"-\").toLowerCase();\nexport const formatCSSValue = (prop: string, val: any) => typeof val === 'number' && !number_val_props.includes(prop) ? `${val}px` : val\n\nconst PREFIXES = ['webkit', 'moz', 'ms', 'o'];\nlet _declaration: CSSStyleDeclaration;\nconst PREFIXCACHE = new Map();\n\nexport const cssPrefix = (prop: string, value: string): { prop: string, value: string } => {\n value = formatCSSValue(prop, value);\n prop = formatCSSProp(prop);\n\n if (typeof window === 'undefined') {\n return { prop, value };\n }\n\n const declaration = _declaration || (_declaration = document.createElement(\"div\").style);\n value = value?.toString();\n\n // Check if the property and value work as is\n if (declaration.setProperty(prop, value), declaration.getPropertyValue(prop) === value) {\n return { prop, value };\n }\n\n // Check cached property and value prefix\n const cached = PREFIXCACHE.get(prop);\n if (cached) {\n return { prop: cached._prop, value: `${cached._vprefix}${value}` };\n }\n\n let _prop = prop;\n let _value = value;\n let _vprefix = '';\n\n // Try property prefixes\n const camelCaseProp = prop.includes('-') ? prop.replace(/-([a-z])/g, (_, c) => c.toUpperCase()) : prop;\n for (const prefix of PREFIXES) {\n if (declaration[`${prefix}${camelCaseProp}` as any] !== undefined) {\n _prop = `-${prefix}-${prop}`;\n break;\n }\n }\n\n // Check if prefixed property works with the value\n declaration.setProperty(_prop, value);\n if (!declaration.getPropertyValue(_prop)) {\n for (const prefix of PREFIXES) {\n const prefixedValue = `-${prefix}-${value}`;\n if (declaration.setProperty(_prop, prefixedValue), declaration.getPropertyValue(_prop) === prefixedValue) {\n _value = prefixedValue;\n _vprefix = `-${prefix}-`;\n break;\n }\n }\n }\n\n PREFIXCACHE.set(prop, { _prop, _vprefix });\n return { prop: _prop, value: _value };\n};\n\nexport const style = <Aliases, BreakpointKeys extends string>(_css: CSSProps<Aliases, BreakpointKeys>, cls?: string, opt?: CSSOptionProps<Aliases, BreakpointKeys>) => {\n let cachekey\n let classname = cls\n if (!cls) {\n cachekey = JSON.stringify(_css)\n const has = CSSFactory.get(cachekey)\n if (has) {\n has.cache = true\n return has\n }\n classname = (opt?.classPrefix || \"\") + uid(cachekey)\n } else if (typeof cls !== 'string') {\n throw new Error(`Invalid class name: ${cls}`)\n }\n\n let stack: any = [`${classname}{`]\n let medias: any = {}\n let skiped: any = {}\n\n for (let prop in _css) {\n let val = (_css as any)[prop]\n let firstChar = prop.charAt(0)\n if (firstChar === '&') {\n let ncls = prop.replace(/&/g, classname as string)\n const r: any = style(val, ncls, opt)\n if (opt?.skipProps) {\n skiped = {\n ...skiped,\n ...r.skiped\n }\n }\n stack.push(r.stack)\n } else if (firstChar === '@') {\n if (prop.startsWith(\"@global\") || prop.startsWith(\"@keyframes\")) {\n let _css = ''\n for (let selector in val) {\n let r: any = style(val[selector], selector, opt)\n _css += r.stack\n if (opt?.skipProps) {\n skiped = {\n ...skiped,\n ...r.skiped\n }\n }\n }\n if (prop.startsWith(\"@keyframes\")) {\n stack.push(`${prop}{${_css}}`)\n } else {\n stack.push(_css)\n }\n } else {\n let r: any = style(val, classname, opt)\n const atcss = prop + \"{\" + r.stack + \"}\"\n stack.push(atcss)\n if (opt?.skipProps) {\n skiped = {\n ...skiped,\n ...r.skiped\n }\n }\n }\n } else if (typeof val === 'object') {\n for (let media in val) {\n if (typeof val[media] === 'object' || typeof val[media] === 'function' || Array.isArray(val[media])) {\n throw new Error(`Invalid css value: ${val[media]}`);\n }\n let breakpoint = media\n let isNumber = !isNaN(parseInt(breakpoint))\n if (!isNumber) {\n if (opt?.breakpoints && !isNaN(parseInt((opt.breakpoints as any)[media]))) {\n breakpoint = opt.breakpoints[media as BreakpointKeys].toString()\n } else {\n throw new Error(`Invalid breakpoint prop: ${media}`);\n }\n }\n let _css = { [prop]: val[media] }\n let r: any = style(_css, classname, opt)\n let _style = r.stack\n let mediakey = `@media (min-width: ${breakpoint}px)`\n medias[mediakey] = medias[mediakey] ? medias[mediakey] + _style : _style\n if (opt?.skipProps) {\n skiped = {\n ...skiped,\n ...r.skiped\n }\n }\n }\n } else {\n if (opt?.skipProps && opt.skipProps(prop, val)) {\n if (!skiped[classname as string]) skiped[classname as string] = []\n skiped[classname as string].push(prop)\n continue\n }\n if (opt?.getProps) {\n let _props: any = opt.getProps(prop, val, _css)\n if (_props) {\n let r: any = style(_props, classname, {\n ...opt,\n getProps: undefined\n })\n if (opt?.skipProps) {\n skiped = {\n ...skiped,\n ...r.skiped\n }\n }\n stack.push(r.stack)\n continue;\n }\n }\n if (opt?.getValue) {\n val = opt.getValue(prop, val, _css)\n } else if (opt?.aliases && (opt.aliases as any)[prop]) {\n let _props = (opt.aliases as any)[prop](val)\n if (_props) {\n let r: any = style(_props, classname, opt)\n r.stack = r.stack.replace(`${classname}{`, '').replace(`}`, '')\n stack[0] += r.stack\n skiped[classname as string] = r.skiped\n continue;\n }\n }\n let p = cssPrefix(prop, val)\n stack[0] += `${p.prop}:${p.value};`\n }\n }\n stack[0] += \"}\"\n if (stack[0] === `${classname}{}`) {\n stack[0] = \"\"\n }\n stack = stack.join('')\n for (let media in medias) {\n stack += `${media}{${medias[media].replace(new RegExp(`}\\\\${classname}\\\\{`, 'g'), '')}}`\n }\n\n if (cachekey) {\n stack = stack.replace(new RegExp(classname as string, 'g'), `.${classname}`)\n const r = {\n cache: false,\n cachekey,\n classname: classname as string,\n css: stack,\n cssraw: _css,\n skiped,\n getStyleTag: () => document?.querySelector(`[data-oncss=\"${classname}\"]`) as HTMLStyleElement | null,\n deleteStyle: () => {\n const tag = document?.querySelector(`[data-oncss=\"${classname}\"]`)\n tag && tag.remove()\n },\n }\n r.toString = () => classname as string\n CSSFactory.set(cachekey, r)\n let inject = opt?.injectStyle || true\n if (inject && typeof window !== 'undefined') {\n if (!document.querySelector(`[data-oncss=\"${classname}\"]`)) {\n const tag = document.createElement(\"style\");\n tag.innerHTML = r.css\n tag.setAttribute(`data-oncss`, classname as string)\n document.head.append(tag)\n }\n }\n return r\n }\n return { stack, skiped }\n}"],
|
|
5
|
+
"mappings": "AAEA,MAAM,UAAe,OAAO,WAAW,cAAc,SAAS;AAC9D,QAAQ,UAAU,QAAQ,WAAW,oBAAI,IAA4B;AAC9D,MAAM,aAAa,QAAQ;AAE3B,MAAM,MAAM,CAAC,QAAgB;AAChC,MAAI,OAAO,GAAG,MAAM,IAAI;AACxB,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC1B,YAAS,QAAQ,KAAK,OAAQ,IAAI,WAAW,CAAC;AAC9C,YAAQ;AAAA,EACZ;AACA,MAAI,KAAK,KAAK,SAAS,EAAE,EAAE,MAAM,GAAG,EAAE,QAAQ,MAAM,EAAE;AACtD,MAAI,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,EAAG,MAAK,MAAM;AACzC,SAAO;AACX;AAEA,MAAM,mBAAmB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ;AAEO,MAAM,gBAAgB,CAAC,SAAiB,KAAK,MAAM,WAAW,EAAE,KAAK,GAAG,EAAE,YAAY;AACtF,MAAM,iBAAiB,CAAC,MAAc,QAAa,OAAO,QAAQ,YAAY,CAAC,iBAAiB,SAAS,IAAI,IAAI,GAAG,GAAG,OAAO;AAErI,MAAM,WAAW,CAAC,UAAU,OAAO,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AE,QAAQ,IAAI,OAAO,MAAM,SAAS,OAAO,GAAG,GAAG,EAAE,CAAC;AAAA,EACzF;AAEA,MAAI,UAAU;AACV,YAAQ,MAAM,QAAQ,IAAI,OAAO,WAAqB,GAAG,GAAG,IAAI,SAAS,EAAE;AAC3E,UAAM,IAAI;AAAA,MACN,OAAO;AAAA,MACP;AAAA,MACA;AAAA,MACA,KAAK;AAAA,MACL,QAAQ;AAAA,MACR;AAAA,MACA,aAAa,MAAM,UAAU,cAAc,gBAAgB,SAAS,IAAI;AAAA,MACxE,aAAa,MAAM;AACf,cAAM,MAAM,UAAU,cAAc,gBAAgB,SAAS,IAAI;AACjE,eAAO,IAAI,OAAO;AAAA,MACtB;AAAA,IACJ;AACA,MAAE,WAAW,MAAM;AACnB,eAAW,IAAI,UAAU,CAAC;AAC1B,QAAI,SAAS,KAAK,eAAe;AACjC,QAAI,UAAU,OAAO,WAAW,aAAa;AACzC,UAAI,CAAC,SAAS,cAAc,gBAAgB,SAAS,IAAI,GAAG;AACxD,cAAM,MAAM,SAAS,cAAc,OAAO;AAC1C,YAAI,YAAY,EAAE;AAClB,YAAI,aAAa,cAAc,SAAmB;AAClD,iBAAS,KAAK,OAAO,GAAG;AAAA,MAC5B;AAAA,IACJ;AACA,WAAO;AAAA,EACX;AACA,SAAO,EAAE,OAAO,OAAO;AAC3B;",
|
|
6
6
|
"names": ["_css"]
|
|
7
7
|
}
|
package/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { style } from \"./core\";\
|
|
4
|
+
"sourcesContent": ["import { style } from \"./core\";\nimport { CSSProps, CSSOptionProps, CSSFactoryType } from \"./types\";\nexport { CSSFactory, formatCSSProp, formatCSSValue } from './core'\nexport * from './types'\n\nexport const css = <Aliases, BreakpointKeys extends string>(_css: CSSProps<Aliases, BreakpointKeys>, options?: CSSOptionProps<Aliases, BreakpointKeys>): CSSFactoryType => style<Aliases, BreakpointKeys>(_css, undefined, options) as any\nexport default css"],
|
|
5
5
|
"mappings": "AAAA,SAAS,aAAa;AAEtB,SAAS,YAAY,eAAe,sBAAsB;AAC1D,cAAc;AAEP,MAAM,MAAM,CAAyC,MAAyC,YAAsE,MAA+B,MAAM,QAAW,OAAO;AAClO,IAAO,gBAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,398 +1,397 @@
|
|
|
1
|
-
<p align="center">
|
|
2
|
-
<img width="120" src="https://raw.githubusercontent.com/devnax/oncss/main/logo.png" alt="ONCSS Logo">
|
|
3
|
-
</p>
|
|
4
|
-
|
|
5
|
-
<h1 align="center">ONCSS</h1>
|
|
6
|
-
|
|
7
|
-
`oncss` is a CSS-in-JS library that provides developers with a powerful `css` function to style their web applications. It enables modern styling techniques, including nested selectors, responsive design, and dynamic keyframes, all while offering seamless integration with JavaScript frameworks like React.
|
|
8
|
-
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## Installation
|
|
12
|
-
|
|
13
|
-
Install the `oncss` package via npm:
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
npm install oncss
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Import the `css` function in your project:
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
import css from 'oncss';
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
---
|
|
26
|
-
|
|
27
|
-
## Core Concept: The `css` Function
|
|
28
|
-
|
|
29
|
-
The `css` function is the heart of `oncss`, designed to dynamically generate and inject CSS into your application. It supports:
|
|
30
|
-
|
|
31
|
-
- **CSS Properties**: Use standard CSS properties and values.
|
|
32
|
-
- **Nested Selectors**: Apply styles to child elements or states using `&`.
|
|
33
|
-
- **Media Queries**: Implement responsive designs with `@media` rules.
|
|
34
|
-
- **Keyframes**: Create animations with `@keyframes`.
|
|
35
|
-
- **Global Styles**: Apply styles globally with `@global`.
|
|
36
|
-
- **Custom Breakpoints**: Define reusable breakpoints for responsiveness.
|
|
37
|
-
- **Other At-Rules**: Utilize additional at-rules like `@container`, `@layer`, and `@supports`.
|
|
38
|
-
|
|
39
|
-
### Basic Example
|
|
40
|
-
|
|
41
|
-
```typescript
|
|
42
|
-
const buttonStyles = css({
|
|
43
|
-
backgroundColor: 'blue',
|
|
44
|
-
color: 'white',
|
|
45
|
-
padding: '10px 20px',
|
|
46
|
-
borderRadius: '5px',
|
|
47
|
-
'&:hover': {
|
|
48
|
-
backgroundColor: 'darkblue',
|
|
49
|
-
},
|
|
50
|
-
'@media (min-width: 768px)': {
|
|
51
|
-
padding: '15px 30px',
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
console.log(buttonStyles);
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
---
|
|
59
|
-
|
|
60
|
-
## Configuration Options
|
|
61
|
-
|
|
62
|
-
The `css` function can be customized through an options object:
|
|
63
|
-
|
|
64
|
-
### Available Properties
|
|
65
|
-
|
|
66
|
-
| Property | Type | Description |
|
|
67
|
-
| ------------- | ---------- | ------------------------------------------ |
|
|
68
|
-
| `classPrefix` | `string` | Adds a prefix to generated class names. |
|
|
69
|
-
| `breakpoints` | `object` | Custom breakpoints for responsive designs. |
|
|
70
|
-
| `aliases` | `object` | Custom shorthand properties for CSS rules. |
|
|
71
|
-
| `injectStyle` | `boolean` | Controls whether styles are auto-injected. |
|
|
72
|
-
| `skipProps` | `function` | Filters out unwanted properties. |
|
|
73
|
-
| `getValue` | `function` | Transforms property values dynamically. |
|
|
74
|
-
| `getProps` | `function` | Customizes specific property handling. |
|
|
75
|
-
|
|
76
|
-
### Example with Options
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
const styles = css({
|
|
80
|
-
fontSize: 16,
|
|
81
|
-
padding: 10,
|
|
82
|
-
}, {
|
|
83
|
-
classPrefix: 'myprefix',
|
|
84
|
-
breakpoints: {
|
|
85
|
-
sm: 480,
|
|
86
|
-
md: 768,
|
|
87
|
-
lg: 1024,
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Using Breakpoints
|
|
93
|
-
|
|
94
|
-
You can use the defined breakpoints in your styles to create responsive designs:
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
const responsiveStyles = css({
|
|
98
|
-
fontSize: 14,
|
|
99
|
-
padding: {
|
|
100
|
-
sm: 12,
|
|
101
|
-
lg: 24
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
}, {
|
|
105
|
-
breakpoints: {
|
|
106
|
-
sm: 480,
|
|
107
|
-
md: 768,
|
|
108
|
-
lg: 1024,
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
---
|
|
114
|
-
|
|
115
|
-
## React Integration
|
|
116
|
-
|
|
117
|
-
oncss integrates seamlessly with React. Simply pass the generated class name to your component.
|
|
118
|
-
|
|
119
|
-
### React Example
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
import React from 'react';
|
|
123
|
-
import css from 'oncss';
|
|
124
|
-
|
|
125
|
-
const buttonStyle = css({
|
|
126
|
-
backgroundColor: 'green',
|
|
127
|
-
color: 'white',
|
|
128
|
-
padding: '10px 20px',
|
|
129
|
-
borderRadius: '8px',
|
|
130
|
-
'&:hover': {
|
|
131
|
-
backgroundColor: 'darkgreen',
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
function Button() {
|
|
136
|
-
return <button className={buttonStyle.toString()}>Click Me</button>;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export default Button;
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
---
|
|
143
|
-
|
|
144
|
-
## Advanced Features
|
|
145
|
-
|
|
146
|
-
### Nested Selectors
|
|
147
|
-
|
|
148
|
-
Apply styles to child elements or pseudo-classes:
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
const cardStyles = css({
|
|
152
|
-
padding: '20px',
|
|
153
|
-
border: '1px solid #ccc',
|
|
154
|
-
'& h1': {
|
|
155
|
-
fontSize: '24px',
|
|
156
|
-
margin: 0,
|
|
157
|
-
},
|
|
158
|
-
'&:hover': {
|
|
159
|
-
boxShadow: '0 4px 8px rgba(0, 0, 0, 0.1)',
|
|
160
|
-
},
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Media Queries
|
|
165
|
-
|
|
166
|
-
Easily add responsive styles:
|
|
167
|
-
|
|
168
|
-
```typescript
|
|
169
|
-
const responsiveStyles = css({
|
|
170
|
-
fontSize: '14px',
|
|
171
|
-
'@media (min-width: 768px)': {
|
|
172
|
-
fontSize: '18px',
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### Keyframes
|
|
178
|
-
|
|
179
|
-
Define and use animations:
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
const animationStyles = css({
|
|
183
|
-
'@keyframes fadeIn': {
|
|
184
|
-
from: { opacity: 0 },
|
|
185
|
-
to: { opacity: 1 },
|
|
186
|
-
},
|
|
187
|
-
animation: 'fadeIn 2s ease-in-out',
|
|
188
|
-
});
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
### Global Styles
|
|
192
|
-
|
|
193
|
-
Apply global styles effortlessly:
|
|
194
|
-
|
|
195
|
-
```typescript
|
|
196
|
-
const globalStyles = css({
|
|
197
|
-
'@global': {
|
|
198
|
-
body: {
|
|
199
|
-
margin: 0,
|
|
200
|
-
fontFamily: 'Arial, sans-serif',
|
|
201
|
-
},
|
|
202
|
-
a: {
|
|
203
|
-
color: 'blue',
|
|
204
|
-
textDecoration: 'none',
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
});
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
---
|
|
211
|
-
|
|
212
|
-
## Supported At-Rules
|
|
213
|
-
|
|
214
|
-
`oncss` supports various CSS at-rules to enhance your styling capabilities. Here is a list of supported at-rules with descriptions:
|
|
215
|
-
|
|
216
|
-
| At-Rule | Description |
|
|
217
|
-
| ------------ | ---------------------------------------------------------------------------- |
|
|
218
|
-
| `@media` | Used for applying styles based on media queries for responsive design. |
|
|
219
|
-
| `@keyframes` | Defines animations that can be applied to elements. |
|
|
220
|
-
| `@global` | Applies styles globally across the entire application. |
|
|
221
|
-
| `@container` | Used for container queries to apply styles based on container size. |
|
|
222
|
-
| `@layer` | Defines style layers to control the order of style application. |
|
|
223
|
-
| `@supports` | Applies styles based on the support of specific CSS features in the browser. |
|
|
224
|
-
|
|
225
|
-
---
|
|
226
|
-
|
|
227
|
-
## Server-Side Styling
|
|
228
|
-
|
|
229
|
-
`oncss` supports server-side rendering (SSR) by utilizing the `CSSFactory` to store and manage generated CSS styles. This allows you to extract and inject styles into your server-rendered HTML.
|
|
230
|
-
|
|
231
|
-
### Example with React
|
|
232
|
-
|
|
233
|
-
Here's an example of how to use `oncss` for server-side rendering with React:
|
|
234
|
-
|
|
235
|
-
```tsx
|
|
236
|
-
import React from 'react';
|
|
237
|
-
import ReactDOMServer from 'react-dom/server';
|
|
238
|
-
import css, { CSSFactory } from 'oncss';
|
|
239
|
-
|
|
240
|
-
const buttonStyle = css({
|
|
241
|
-
backgroundColor: 'blue',
|
|
242
|
-
color: 'white',
|
|
243
|
-
padding: '10px 20px',
|
|
244
|
-
borderRadius: '5px',
|
|
245
|
-
'&:hover': {
|
|
246
|
-
backgroundColor: 'darkblue',
|
|
247
|
-
},
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
function Button() {
|
|
251
|
-
return <button className={buttonStyle}>Click Me</button>;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const App = () => (
|
|
255
|
-
<div>
|
|
256
|
-
<Button />
|
|
257
|
-
</div>
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
// Render the component to a string
|
|
261
|
-
const html = ReactDOMServer.renderToString(<App />);
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
<
|
|
272
|
-
<
|
|
273
|
-
<meta
|
|
274
|
-
<
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
<
|
|
279
|
-
|
|
280
|
-
</
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
<
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
<
|
|
385
|
-
<a href="https://
|
|
386
|
-
<a href="https://
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
<
|
|
395
|
-
<a target="_blank" href="https://
|
|
396
|
-
<a target="_blank" href="https://www.
|
|
397
|
-
<a target="_blank" href="https://www.
|
|
398
|
-
<a target="_blank" href="https://www.instagram.com/devnaxx" style="display: inline-block;"><img src="https://img.shields.io/badge/instagram-logo?style=for-the-badge&logo=instagram&logoColor=white&color=%23F35369" alt="instagram" /></a></p>
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="120" src="https://raw.githubusercontent.com/devnax/oncss/main/logo.png" alt="ONCSS Logo">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">ONCSS</h1>
|
|
6
|
+
|
|
7
|
+
`oncss` is a CSS-in-JS library that provides developers with a powerful `css` function to style their web applications. It enables modern styling techniques, including nested selectors, responsive design, and dynamic keyframes, all while offering seamless integration with JavaScript frameworks like React.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install the `oncss` package via npm:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install oncss
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Import the `css` function in your project:
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import css from 'oncss';
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Core Concept: The `css` Function
|
|
28
|
+
|
|
29
|
+
The `css` function is the heart of `oncss`, designed to dynamically generate and inject CSS into your application. It supports:
|
|
30
|
+
|
|
31
|
+
- **CSS Properties**: Use standard CSS properties and values.
|
|
32
|
+
- **Nested Selectors**: Apply styles to child elements or states using `&`.
|
|
33
|
+
- **Media Queries**: Implement responsive designs with `@media` rules.
|
|
34
|
+
- **Keyframes**: Create animations with `@keyframes`.
|
|
35
|
+
- **Global Styles**: Apply styles globally with `@global`.
|
|
36
|
+
- **Custom Breakpoints**: Define reusable breakpoints for responsiveness.
|
|
37
|
+
- **Other At-Rules**: Utilize additional at-rules like `@container`, `@layer`, and `@supports`.
|
|
38
|
+
|
|
39
|
+
### Basic Example
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const buttonStyles = css({
|
|
43
|
+
backgroundColor: 'blue',
|
|
44
|
+
color: 'white',
|
|
45
|
+
padding: '10px 20px',
|
|
46
|
+
borderRadius: '5px',
|
|
47
|
+
'&:hover': {
|
|
48
|
+
backgroundColor: 'darkblue',
|
|
49
|
+
},
|
|
50
|
+
'@media (min-width: 768px)': {
|
|
51
|
+
padding: '15px 30px',
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
console.log(buttonStyles);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Configuration Options
|
|
61
|
+
|
|
62
|
+
The `css` function can be customized through an options object:
|
|
63
|
+
|
|
64
|
+
### Available Properties
|
|
65
|
+
|
|
66
|
+
| Property | Type | Description |
|
|
67
|
+
| ------------- | ---------- | ------------------------------------------ |
|
|
68
|
+
| `classPrefix` | `string` | Adds a prefix to generated class names. |
|
|
69
|
+
| `breakpoints` | `object` | Custom breakpoints for responsive designs. |
|
|
70
|
+
| `aliases` | `object` | Custom shorthand properties for CSS rules. |
|
|
71
|
+
| `injectStyle` | `boolean` | Controls whether styles are auto-injected. |
|
|
72
|
+
| `skipProps` | `function` | Filters out unwanted properties. |
|
|
73
|
+
| `getValue` | `function` | Transforms property values dynamically. |
|
|
74
|
+
| `getProps` | `function` | Customizes specific property handling. |
|
|
75
|
+
|
|
76
|
+
### Example with Options
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const styles = css({
|
|
80
|
+
fontSize: 16,
|
|
81
|
+
padding: 10,
|
|
82
|
+
}, {
|
|
83
|
+
classPrefix: 'myprefix',
|
|
84
|
+
breakpoints: {
|
|
85
|
+
sm: 480,
|
|
86
|
+
md: 768,
|
|
87
|
+
lg: 1024,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Using Breakpoints
|
|
93
|
+
|
|
94
|
+
You can use the defined breakpoints in your styles to create responsive designs:
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
const responsiveStyles = css({
|
|
98
|
+
fontSize: 14,
|
|
99
|
+
padding: {
|
|
100
|
+
sm: 12,
|
|
101
|
+
lg: 24
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
}, {
|
|
105
|
+
breakpoints: {
|
|
106
|
+
sm: 480,
|
|
107
|
+
md: 768,
|
|
108
|
+
lg: 1024,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## React Integration
|
|
116
|
+
|
|
117
|
+
oncss integrates seamlessly with React. Simply pass the generated class name to your component.
|
|
118
|
+
|
|
119
|
+
### React Example
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import React from 'react';
|
|
123
|
+
import css from 'oncss';
|
|
124
|
+
|
|
125
|
+
const buttonStyle = css({
|
|
126
|
+
backgroundColor: 'green',
|
|
127
|
+
color: 'white',
|
|
128
|
+
padding: '10px 20px',
|
|
129
|
+
borderRadius: '8px',
|
|
130
|
+
'&:hover': {
|
|
131
|
+
backgroundColor: 'darkgreen',
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
function Button() {
|
|
136
|
+
return <button className={buttonStyle.toString()}>Click Me</button>;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default Button;
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Advanced Features
|
|
145
|
+
|
|
146
|
+
### Nested Selectors
|
|
147
|
+
|
|
148
|
+
Apply styles to child elements or pseudo-classes:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
const cardStyles = css({
|
|
152
|
+
padding: '20px',
|
|
153
|
+
border: '1px solid #ccc',
|
|
154
|
+
'& h1': {
|
|
155
|
+
fontSize: '24px',
|
|
156
|
+
margin: 0,
|
|
157
|
+
},
|
|
158
|
+
'&:hover': {
|
|
159
|
+
boxShadow: '0 4px 8px rgba(0, 0, 0, 0.1)',
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Media Queries
|
|
165
|
+
|
|
166
|
+
Easily add responsive styles:
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
const responsiveStyles = css({
|
|
170
|
+
fontSize: '14px',
|
|
171
|
+
'@media (min-width: 768px)': {
|
|
172
|
+
fontSize: '18px',
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Keyframes
|
|
178
|
+
|
|
179
|
+
Define and use animations:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
const animationStyles = css({
|
|
183
|
+
'@keyframes fadeIn': {
|
|
184
|
+
from: { opacity: 0 },
|
|
185
|
+
to: { opacity: 1 },
|
|
186
|
+
},
|
|
187
|
+
animation: 'fadeIn 2s ease-in-out',
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Global Styles
|
|
192
|
+
|
|
193
|
+
Apply global styles effortlessly:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
const globalStyles = css({
|
|
197
|
+
'@global': {
|
|
198
|
+
body: {
|
|
199
|
+
margin: 0,
|
|
200
|
+
fontFamily: 'Arial, sans-serif',
|
|
201
|
+
},
|
|
202
|
+
a: {
|
|
203
|
+
color: 'blue',
|
|
204
|
+
textDecoration: 'none',
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Supported At-Rules
|
|
213
|
+
|
|
214
|
+
`oncss` supports various CSS at-rules to enhance your styling capabilities. Here is a list of supported at-rules with descriptions:
|
|
215
|
+
|
|
216
|
+
| At-Rule | Description |
|
|
217
|
+
| ------------ | ---------------------------------------------------------------------------- |
|
|
218
|
+
| `@media` | Used for applying styles based on media queries for responsive design. |
|
|
219
|
+
| `@keyframes` | Defines animations that can be applied to elements. |
|
|
220
|
+
| `@global` | Applies styles globally across the entire application. |
|
|
221
|
+
| `@container` | Used for container queries to apply styles based on container size. |
|
|
222
|
+
| `@layer` | Defines style layers to control the order of style application. |
|
|
223
|
+
| `@supports` | Applies styles based on the support of specific CSS features in the browser. |
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## Server-Side Styling
|
|
228
|
+
|
|
229
|
+
`oncss` supports server-side rendering (SSR) by utilizing the `CSSFactory` to store and manage generated CSS styles. This allows you to extract and inject styles into your server-rendered HTML.
|
|
230
|
+
|
|
231
|
+
### Example with React
|
|
232
|
+
|
|
233
|
+
Here's an example of how to use `oncss` for server-side rendering with React:
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
import React from 'react';
|
|
237
|
+
import ReactDOMServer from 'react-dom/server';
|
|
238
|
+
import css, { CSSFactory } from 'oncss';
|
|
239
|
+
|
|
240
|
+
const buttonStyle = css({
|
|
241
|
+
backgroundColor: 'blue',
|
|
242
|
+
color: 'white',
|
|
243
|
+
padding: '10px 20px',
|
|
244
|
+
borderRadius: '5px',
|
|
245
|
+
'&:hover': {
|
|
246
|
+
backgroundColor: 'darkblue',
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
function Button() {
|
|
251
|
+
return <button className={buttonStyle}>Click Me</button>;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const App = () => (
|
|
255
|
+
<div>
|
|
256
|
+
<Button />
|
|
257
|
+
</div>
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
// Render the component to a string
|
|
261
|
+
const html = ReactDOMServer.renderToString(<App />);
|
|
262
|
+
|
|
263
|
+
let styles: any = Array.from(CSSFactory.values()).map((style) => {
|
|
264
|
+
return `<style data-oncss="${style.classname}">${style.css}</style>`
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
// Inject the styles into the HTML
|
|
268
|
+
const fullHtml = `
|
|
269
|
+
<!DOCTYPE html>
|
|
270
|
+
<html lang="en">
|
|
271
|
+
<head>
|
|
272
|
+
<meta charset="UTF-8">
|
|
273
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
274
|
+
<title>SSR with oncss</title>
|
|
275
|
+
${styles}
|
|
276
|
+
</head>
|
|
277
|
+
<body>
|
|
278
|
+
<div id="root">${html}</div>
|
|
279
|
+
</body>
|
|
280
|
+
</html>
|
|
281
|
+
`;
|
|
282
|
+
|
|
283
|
+
console.log(fullHtml);
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
In this example, the `CSSFactory` is used to collect all generated CSS styles during the server-side rendering process. These styles are then injected into the HTML document, ensuring that the styles are applied correctly when the page is loaded in the browser.
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## Utility Functions
|
|
291
|
+
|
|
292
|
+
### CSSFactory
|
|
293
|
+
|
|
294
|
+
`CSSFactory` is a global cache for storing generated CSS styles. It helps in reusing styles and avoiding redundant style generation.
|
|
295
|
+
|
|
296
|
+
### formatCSSProp
|
|
297
|
+
|
|
298
|
+
`formatCSSProp` is a utility function that converts camelCase CSS property names to kebab-case.
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import { formatCSSProp } from 'oncss';
|
|
302
|
+
|
|
303
|
+
const formattedProp = formatCSSProp('backgroundColor');
|
|
304
|
+
console.log(formattedProp); // 'background-color'
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### formatCSSValue
|
|
308
|
+
|
|
309
|
+
`formatCSSValue` is a utility function that formats CSS values, adding units like `px` where necessary.
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
import { formatCSSValue } from 'oncss';
|
|
313
|
+
|
|
314
|
+
const formattedValue = formatCSSValue('width', 100);
|
|
315
|
+
console.log(formattedValue); // '100px'
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
---
|
|
319
|
+
|
|
320
|
+
## TypeScript Integration
|
|
321
|
+
|
|
322
|
+
`oncss` provides full TypeScript support, allowing you to define types for your CSS properties and options.
|
|
323
|
+
|
|
324
|
+
### Defining CSS Properties
|
|
325
|
+
|
|
326
|
+
You can define the types for your CSS properties using the `CSSProps` type:
|
|
327
|
+
|
|
328
|
+
```typescript
|
|
329
|
+
import { CSSProps } from 'oncss';
|
|
330
|
+
|
|
331
|
+
interface MyAliases {
|
|
332
|
+
customColor?: string;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const styles: CSSProps<MyAliases, 'sm' | 'md' | 'lg'> = {
|
|
336
|
+
backgroundColor: 'blue',
|
|
337
|
+
customColor: 'red',
|
|
338
|
+
'@media (min-width: 768px)': {
|
|
339
|
+
backgroundColor: 'green',
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### Using Options with Types
|
|
345
|
+
|
|
346
|
+
You can also define types for the options object:
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
import { CSSOptionProps } from 'oncss';
|
|
350
|
+
|
|
351
|
+
const options: CSSOptionProps<MyAliases, 'sm' | 'md' | 'lg'> = {
|
|
352
|
+
classPrefix: 'myprefix',
|
|
353
|
+
breakpoints: {
|
|
354
|
+
sm: 480,
|
|
355
|
+
md: 768,
|
|
356
|
+
lg: 1024,
|
|
357
|
+
},
|
|
358
|
+
aliases: {
|
|
359
|
+
customColor: (value) => ({ color: value }),
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const styles = css({
|
|
364
|
+
fontSize: 16,
|
|
365
|
+
padding: 10,
|
|
366
|
+
}, options);
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
## Conclusion
|
|
372
|
+
|
|
373
|
+
`oncss` simplifies styling for modern web applications. Its robust feature set, from responsive design to keyframe animations, makes it an invaluable tool for developers.
|
|
374
|
+
|
|
375
|
+
## Author
|
|
376
|
+
|
|
377
|
+
<table>
|
|
378
|
+
<tr>
|
|
379
|
+
<td>
|
|
380
|
+
<img src="https://raw.githubusercontent.com/devnax/devnax/main/me-circle-200.png" alt="devnax" width="100" height="100">
|
|
381
|
+
</td>
|
|
382
|
+
<td>
|
|
383
|
+
<strong>Naxrul Ahmed</strong><br>
|
|
384
|
+
<a href="https://github.com/devnax">GitHub Profile</a><br>
|
|
385
|
+
<a href="https://www.npmjs.com/~devnax">npm Profile</a><br>
|
|
386
|
+
<a href="https://github.com/devnax/open-source">Open Source Projects</a>
|
|
387
|
+
</td>
|
|
388
|
+
</tr>
|
|
389
|
+
</table>
|
|
390
|
+
|
|
391
|
+
<h2>⚡️ Where to find me</h2>
|
|
392
|
+
|
|
393
|
+
<p><a target="_blank" href="mailto:devnaxrul@gmail.com" style="display: inline-block;"><img src="https://img.shields.io/badge/-Email-05122A?style=for-the-badge&logo=gmail&logoColor=white&color=orange" alt="gmail" /></a>
|
|
394
|
+
<a target="_blank" href="https://twitter.com/devnaxx" style="display: inline-block;"><img src="https://img.shields.io/badge/twitter-x?style=for-the-badge&logo=x&logoColor=white&color=%230f1419" alt="twitter" /></a>
|
|
395
|
+
<a target="_blank" href="https://www.linkedin.com/in/devnax" style="display: inline-block;"><img src="https://img.shields.io/badge/linkedin-logo?style=for-the-badge&logo=linkedin&logoColor=white&color=%230a77b6" alt="linkedin" /></a>
|
|
396
|
+
<a target="_blank" href="https://www.facebook.com/devnax" style="display: inline-block;"><img src="https://img.shields.io/badge/facebook-logo?style=for-the-badge&logo=facebook&logoColor=white&color=%230866ff" alt="facebook" /></a>
|
|
397
|
+
<a target="_blank" href="https://www.instagram.com/devnaxx" style="display: inline-block;"><img src="https://img.shields.io/badge/instagram-logo?style=for-the-badge&logo=instagram&logoColor=white&color=%23F35369" alt="instagram" /></a></p>
|
package/types.d.ts
CHANGED
|
@@ -33,15 +33,16 @@ type CSSValue<Aliases, BreakpointKeys extends string> = {
|
|
|
33
33
|
};
|
|
34
34
|
export type CSSPropsWithoutGlobal<Aliases, BreakpointKeys extends string> = CSSValue<Aliases, BreakpointKeys> | KeyframesCSS<Aliases> | MediaCSS<Aliases, BreakpointKeys> | CSSNestedSelectors<Aliases, BreakpointKeys> | CSSNestedSelectorsWithoutChild<Aliases, BreakpointKeys>;
|
|
35
35
|
export type CSSProps<Aliases, BreakpointKeys extends string> = GlobalCSS<Aliases, BreakpointKeys> | CSSPropsWithoutGlobal<Aliases, BreakpointKeys>;
|
|
36
|
-
export type AliasFn<Aliases> = (
|
|
36
|
+
export type AliasFn<Aliases> = (value: string | number) => CSSValueWithoutBreakpoint<Aliases>;
|
|
37
|
+
export type OptionAliases<Aliases> = {
|
|
38
|
+
[key in keyof Aliases]: AliasFn<Aliases>;
|
|
39
|
+
};
|
|
37
40
|
export interface CSSOptionProps<Aliases, BreakpointKeys extends string> {
|
|
38
41
|
classPrefix?: string;
|
|
39
42
|
breakpoints?: {
|
|
40
43
|
[key in BreakpointKeys]: number;
|
|
41
44
|
};
|
|
42
|
-
aliases?:
|
|
43
|
-
[key in keyof Aliases]: AliasFn<Aliases>;
|
|
44
|
-
};
|
|
45
|
+
aliases?: OptionAliases<Aliases>;
|
|
45
46
|
injectStyle?: boolean;
|
|
46
47
|
skipProps?: (prop: string, value: string | number) => boolean;
|
|
47
48
|
getValue?: (value: string | number, prop: string, css: CSSProps<Aliases, BreakpointKeys>) => (string | number);
|