-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
165 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@suid/css": patch | ||
"@suid/system": patch | ||
--- | ||
|
||
Refactor `css` package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import render from "./render"; | ||
import randomString from "@suid/utils/randomString"; | ||
import resolveFunction from "@suid/utils/resolveFunction"; | ||
import toArray from "@suid/utils/toArray"; | ||
|
||
export type StyleObject = { | ||
id: string; | ||
name: string; | ||
className: string; | ||
rules: string; | ||
}; | ||
|
||
export type StyleObjectCache = Map<string, StyleObject>; | ||
|
||
export type StyleProps = | ||
| Record<string, any> | ||
| string | ||
| (Record<string, any> | string)[] | ||
| (() => Record<string, any> | string | (Record<string, any> | string)[]); | ||
|
||
export type StyleObjectOptions = { | ||
name: string; | ||
props: StyleProps; | ||
extraProperties?: Record<string, (value: any) => any>; | ||
cache?: StyleObjectCache; | ||
}; | ||
|
||
function create(name: string, rules: string) { | ||
const id = randomString().slice(0, 6); | ||
return { | ||
id, | ||
name: name, | ||
className: `${name}-${id}`, | ||
rules: rules.replaceAll(`$id`, `${id}`), | ||
}; | ||
} | ||
|
||
function createStyleObject(options: StyleObjectOptions) { | ||
const className = `${options.name}-$id`; | ||
const propsValues = toArray(resolveFunction(options.props)); | ||
const rules = propsValues | ||
.map((v) => | ||
typeof v === "string" | ||
? `.${className} {\n${v}\n}` | ||
: render(v, [`.${className}`], { | ||
extraProperties: options.extraProperties, | ||
}).join("\n") | ||
) | ||
.join("\n"); | ||
|
||
const styleObject = options.cache?.get(rules) || create(options.name, rules); | ||
|
||
if (options.cache) options.cache.set(rules, styleObject); | ||
|
||
return styleObject; | ||
} | ||
|
||
export default createStyleObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import createStyleElement from "./createStyleElement"; | ||
import registerStyleElementUsage from "./registerStyleElementUsage"; | ||
import setStyleElementText from "./setStyleElementText"; | ||
|
||
function appendStyleElement( | ||
css: string | string[], | ||
id?: string | false | ||
): { | ||
element: HTMLStyleElement; | ||
id: string | false | undefined; | ||
} { | ||
if (Array.isArray(css)) css = css.join("\n"); | ||
const head = document.head || document.getElementsByTagName("head")[0]; | ||
const prevElement = id && document.getElementById(id); | ||
if (prevElement && prevElement instanceof HTMLStyleElement) { | ||
setStyleElementText(prevElement, css); | ||
registerStyleElementUsage(prevElement); | ||
return { element: prevElement, id }; | ||
} else { | ||
if (prevElement) prevElement.remove(); | ||
const element = createStyleElement(css, id); | ||
registerStyleElementUsage(element); | ||
head.appendChild(element); | ||
return { element, id }; | ||
} | ||
} | ||
|
||
export default appendStyleElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import setStyleElementText from "./setStyleElementText"; | ||
|
||
function createStyleElement(css: string, id?: string | false) { | ||
const element = document.createElement("style"); | ||
if (id) element.setAttribute("id", id); | ||
element.type = "text/css"; | ||
setStyleElementText(element, css); | ||
return element; | ||
} | ||
|
||
export default createStyleElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function findStyleElement(id: string) { | ||
return document.getElementById(id) as HTMLStyleElement | undefined; | ||
} | ||
|
||
export default findStyleElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function registerStyleElementUsage(style: HTMLStyleElement) { | ||
let uses = Number(style.getAttribute("data-uses")); | ||
uses++; | ||
style.setAttribute("data-uses", uses.toString()); | ||
} | ||
|
||
export default registerStyleElementUsage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function setStyleElementText(element: HTMLStyleElement, text: string) { | ||
if ("styleSheet" in element) { | ||
(element as any)["styleSheet"].cssText = text; | ||
} else { | ||
element.innerText = ""; | ||
element.appendChild(document.createTextNode(text)); | ||
} | ||
} | ||
|
||
export default setStyleElementText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function unregisterStyleElementUsage(style: HTMLStyleElement) { | ||
let uses = Number(style.getAttribute("data-uses")); | ||
uses--; | ||
if (uses <= 0) { | ||
style.remove(); | ||
} else { | ||
style.setAttribute("data-uses", uses.toString()); | ||
} | ||
} | ||
|
||
export default unregisterStyleElementUsage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters