init
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "@ohmycv/dynamic-css",
|
||||
"version": "0.1.1",
|
||||
"description": "Dynamically injects global CSS.",
|
||||
"homepage": "https://github.com/Renovamen/oh-my-cv/tree/main/packages/dynamic-css",
|
||||
"bugs": {
|
||||
"url": "https://github.com/Renovamen/oh-my-cv"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/Renovamen/oh-my-cv.git",
|
||||
"directory": "packages/dynamic-css"
|
||||
},
|
||||
"license": "MIT",
|
||||
"author": "Renovamen <renovamenzxh@gmail.com>",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"exports": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"require": "./dist/index.js",
|
||||
"import": "./dist/index.mjs"
|
||||
},
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build-fast:pkg": "tsup src/index.ts --format cjs,esm",
|
||||
"build:pkg": "pnpm run build-fast:pkg --dts"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
const sheetsMap = new Map<string, HTMLStyleElement>();
|
||||
|
||||
/**
|
||||
* Dynamically injects CSS into the document. Borrowed from Vite:
|
||||
* https://github.com/vitejs/vite/blob/main/packages/vite/src/client/client.ts
|
||||
*
|
||||
* This used to be implemented using constructable stylesheets, but that was abandoned
|
||||
* due to low performance, see https://github.com/vitejs/vite/pull/11818.
|
||||
*
|
||||
* @param id To make sure the CSS won't override each other.
|
||||
* @param content A string of CSS to inject.
|
||||
*/
|
||||
export const injectCss = (id: string, content: string) => {
|
||||
let style = sheetsMap.get(id);
|
||||
|
||||
if (!style) {
|
||||
style = document.createElement("style");
|
||||
|
||||
style.setAttribute("type", "text/css");
|
||||
style.setAttribute("data-dynamic-css-id", id);
|
||||
style.textContent = content;
|
||||
|
||||
document.head.appendChild(style);
|
||||
} else {
|
||||
style.textContent = content;
|
||||
}
|
||||
|
||||
sheetsMap.set(id, style);
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove the CSS from the document.
|
||||
*
|
||||
* @param id The ID of the CSS to remove.
|
||||
*/
|
||||
export const removeCss = (id: string) => {
|
||||
const style = sheetsMap.get(id);
|
||||
|
||||
if (style) {
|
||||
document.head.removeChild(style);
|
||||
sheetsMap.delete(id);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"include": ["./src"]
|
||||
}
|
||||
Reference in New Issue
Block a user