uni-theme-select 1.0.0 → 1.0.1
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/README.md +240 -0
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +209 -7
- package/dist/index.mjs +195 -5
- package/package.json +12 -3
package/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# uni-theme-select
|
|
2
|
+
|
|
3
|
+
# Uni Theme Toggle
|
|
4
|
+
|
|
5
|
+
A lightweight, plug-and-play theme switcher with built-in custom color support using CSS variables and Coloris.
|
|
6
|
+
|
|
7
|
+
Easily add Dark, Light, Beige, or fully Custom themes to any web application.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
* Predefined themes (Dark, Light, Beige)
|
|
14
|
+
* Custom theme builder (Background, Primary, Secondary colors)
|
|
15
|
+
* CSS Variable based theming
|
|
16
|
+
* Theme persistence via `localStorage`
|
|
17
|
+
* Mount anywhere in DOM
|
|
18
|
+
* Zero framework dependency (Vanilla JS compatible)
|
|
19
|
+
* Lightweight and modular
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install uni-theme-toggle
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
or
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
yarn add uni-theme-toggle
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### Import
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import {
|
|
43
|
+
setTheme,
|
|
44
|
+
loadSavedTheme,
|
|
45
|
+
mountToggleTheme,
|
|
46
|
+
unMountToggleTheme
|
|
47
|
+
} from "uni-theme-toggle";
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
### Mount the Theme Toggle
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
mountToggleTheme({
|
|
56
|
+
containerId: "app" // optional
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
If `containerId` is not provided, the toggle mounts to `document.body`.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
### Load Saved Theme on App Start
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
loadSavedTheme();
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
This restores the previously selected theme from `localStorage`.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
### Manually Set Theme
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
setTheme("dark");
|
|
78
|
+
setTheme("light");
|
|
79
|
+
setTheme("beige");
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
### Set Custom Theme
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
setTheme("custom", {
|
|
88
|
+
bgColor: "#121212",
|
|
89
|
+
primaryColor: "#4f46e5",
|
|
90
|
+
secondaryColor: "#f59e0b"
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## How It Works
|
|
97
|
+
|
|
98
|
+
The package updates CSS variables on the `:root` element:
|
|
99
|
+
|
|
100
|
+
```css
|
|
101
|
+
:root {
|
|
102
|
+
--bgColor: #000;
|
|
103
|
+
--primaryColor: #fff;
|
|
104
|
+
--secondaryColor: #ccc;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Your app should use these variables:
|
|
109
|
+
|
|
110
|
+
```css
|
|
111
|
+
body {
|
|
112
|
+
background-color: var(--bgColor);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
button {
|
|
116
|
+
background-color: var(--primaryColor);
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Available API
|
|
123
|
+
|
|
124
|
+
### `setTheme(name: string, themeProps?: Record<string, string>)`
|
|
125
|
+
|
|
126
|
+
Apply a theme.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
### `getAvailableThemes(): string[]`
|
|
131
|
+
|
|
132
|
+
Returns available predefined themes.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
### `loadSavedTheme()`
|
|
137
|
+
|
|
138
|
+
Loads previously stored theme from `localStorage`.
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
### `mountToggleTheme(options?)`
|
|
143
|
+
|
|
144
|
+
Mounts the theme toggle UI.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
mountToggleTheme({
|
|
148
|
+
containerId?: string
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
### `unMountToggleTheme()`
|
|
155
|
+
|
|
156
|
+
Removes the toggle UI from DOM.
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Architecture
|
|
161
|
+
|
|
162
|
+
* Theme definitions stored in `themes.ts`
|
|
163
|
+
* CSS injected dynamically
|
|
164
|
+
* Color picker powered by Coloris
|
|
165
|
+
* Fully dynamic DOM rendering
|
|
166
|
+
* No global HTML required
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## Customization
|
|
171
|
+
|
|
172
|
+
You can extend predefined themes by editing:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
themes.ts
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Example:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
export const themes = [
|
|
182
|
+
{
|
|
183
|
+
name: "dark",
|
|
184
|
+
properties: {
|
|
185
|
+
bgColor: "#121212",
|
|
186
|
+
primaryColor: "#ffffff",
|
|
187
|
+
secondaryColor: "#999999"
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
];
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## Theme Persistence
|
|
196
|
+
|
|
197
|
+
Selected theme is stored in:
|
|
198
|
+
|
|
199
|
+
```
|
|
200
|
+
localStorage key: "uts-theme"
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## Requirements
|
|
206
|
+
|
|
207
|
+
* Modern browser (supports CSS variables)
|
|
208
|
+
* Bundler that supports CSS imports (Vite, Webpack, etc.)
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Example
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
216
|
+
loadSavedTheme();
|
|
217
|
+
mountToggleTheme();
|
|
218
|
+
});
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Remove Toggle
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
unMountToggleTheme();
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## 🧩 Roadmap
|
|
232
|
+
|
|
233
|
+
* Theme export/import
|
|
234
|
+
* Animation support
|
|
235
|
+
* Multi-theme preview
|
|
236
|
+
* TypeScript definitions improvements
|
|
237
|
+
* React wrapper
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
declare function setTheme(name: string): void;
|
|
2
1
|
declare function getAvailableThemes(): string[];
|
|
3
2
|
declare function loadSavedTheme(): void;
|
|
3
|
+
declare function mountToggleTheme(options?: Record<string, string>): void;
|
|
4
|
+
declare function unMountToggleTheme(): void;
|
|
4
5
|
|
|
5
6
|
type Theme = {
|
|
6
7
|
name: string;
|
|
@@ -9,4 +10,4 @@ type Theme = {
|
|
|
9
10
|
|
|
10
11
|
declare const themes: Theme[];
|
|
11
12
|
|
|
12
|
-
export { type Theme, getAvailableThemes, loadSavedTheme,
|
|
13
|
+
export { type Theme, getAvailableThemes, loadSavedTheme, mountToggleTheme, themes, unMountToggleTheme };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
declare function setTheme(name: string): void;
|
|
2
1
|
declare function getAvailableThemes(): string[];
|
|
3
2
|
declare function loadSavedTheme(): void;
|
|
3
|
+
declare function mountToggleTheme(options?: Record<string, string>): void;
|
|
4
|
+
declare function unMountToggleTheme(): void;
|
|
4
5
|
|
|
5
6
|
type Theme = {
|
|
6
7
|
name: string;
|
|
@@ -9,4 +10,4 @@ type Theme = {
|
|
|
9
10
|
|
|
10
11
|
declare const themes: Theme[];
|
|
11
12
|
|
|
12
|
-
export { type Theme, getAvailableThemes, loadSavedTheme,
|
|
13
|
+
export { type Theme, getAvailableThemes, loadSavedTheme, mountToggleTheme, themes, unMountToggleTheme };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
1
3
|
var __defProp = Object.defineProperty;
|
|
2
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
8
|
var __export = (target, all) => {
|
|
6
9
|
for (var name in all)
|
|
@@ -14,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
17
|
}
|
|
15
18
|
return to;
|
|
16
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
17
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
29
|
|
|
19
30
|
// src/index.ts
|
|
@@ -21,8 +32,9 @@ var index_exports = {};
|
|
|
21
32
|
__export(index_exports, {
|
|
22
33
|
getAvailableThemes: () => getAvailableThemes,
|
|
23
34
|
loadSavedTheme: () => loadSavedTheme,
|
|
24
|
-
|
|
25
|
-
themes: () => themes
|
|
35
|
+
mountToggleTheme: () => mountToggleTheme,
|
|
36
|
+
themes: () => themes,
|
|
37
|
+
unMountToggleTheme: () => unMountToggleTheme
|
|
26
38
|
});
|
|
27
39
|
module.exports = __toCommonJS(index_exports);
|
|
28
40
|
|
|
@@ -51,12 +63,134 @@ var themes = [
|
|
|
51
63
|
"text-color": "#333333",
|
|
52
64
|
"primary-color": "#c19a6b"
|
|
53
65
|
}
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: "custom",
|
|
69
|
+
properties: {
|
|
70
|
+
"bg-color": "",
|
|
71
|
+
"text-color": "",
|
|
72
|
+
"primary-color": ""
|
|
73
|
+
}
|
|
54
74
|
}
|
|
55
75
|
];
|
|
56
76
|
|
|
57
|
-
// src/
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
// src/coreFunctions.ts
|
|
78
|
+
var import_coloris = require("@melloware/coloris/dist/coloris.css");
|
|
79
|
+
var import_coloris2 = __toESM(require("@melloware/coloris"));
|
|
80
|
+
var colorFields = [
|
|
81
|
+
{ id: "bgColor", label: "Background" },
|
|
82
|
+
{ id: "primaryColor", label: "Primary" },
|
|
83
|
+
{ id: "secondaryColor", label: "Secondary" },
|
|
84
|
+
{ id: "borderColor", label: "Border Color" },
|
|
85
|
+
{ id: "textColor", label: "Text Color" }
|
|
86
|
+
];
|
|
87
|
+
function injectStyles() {
|
|
88
|
+
if (document.getElementById("uni-theme-style")) return;
|
|
89
|
+
const style = document.createElement("style");
|
|
90
|
+
style.id = "uni-theme-style";
|
|
91
|
+
style.innerHTML = `
|
|
92
|
+
#uni-theme-toggle {
|
|
93
|
+
position: fixed;
|
|
94
|
+
bottom: 20px;
|
|
95
|
+
right: 20px;
|
|
96
|
+
width: 50px;
|
|
97
|
+
height: 50px;
|
|
98
|
+
background: var(--bg, #222);
|
|
99
|
+
color: var(--text, #fff);
|
|
100
|
+
display: flex;
|
|
101
|
+
align-items: center;
|
|
102
|
+
justify-content: center;
|
|
103
|
+
border-radius: 50%;
|
|
104
|
+
cursor: pointer;
|
|
105
|
+
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
|
|
106
|
+
z-index: 9999;
|
|
107
|
+
font-size:20px;
|
|
108
|
+
transition: right 0.3s ease;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#uni-theme-overlay {
|
|
112
|
+
position: fixed;
|
|
113
|
+
inset: 0;
|
|
114
|
+
background: rgba(0,0,0,0.3);
|
|
115
|
+
opacity: 0;
|
|
116
|
+
pointer-events: none;
|
|
117
|
+
transition: opacity 0.3s ease;
|
|
118
|
+
z-index: 9998;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#uni-theme-overlay.show {
|
|
122
|
+
opacity: 1;
|
|
123
|
+
pointer-events: all;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#uni-theme-panel {
|
|
127
|
+
position: fixed;
|
|
128
|
+
bottom: 20px;
|
|
129
|
+
right: -322px;
|
|
130
|
+
width: 280px;
|
|
131
|
+
background: var(--bg, #fff);
|
|
132
|
+
color: var(--text, #000);
|
|
133
|
+
padding: 20px;
|
|
134
|
+
box-shadow: -4px 0 12px rgba(0,0,0,0.2);
|
|
135
|
+
transition: right 0.3s ease;
|
|
136
|
+
z-index: 9999;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
#uni-theme-root.opened #uni-theme-panel{
|
|
140
|
+
right: 0;
|
|
141
|
+
max-height: 500px;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
#uni-theme-root.opened #uni-theme-toggle{
|
|
145
|
+
right: 322px;
|
|
146
|
+
transition: left 0.3s ease;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#uni-theme-panel button {
|
|
150
|
+
display: block;
|
|
151
|
+
margin: 10px 0;
|
|
152
|
+
padding: 8px;
|
|
153
|
+
width: 100%;
|
|
154
|
+
cursor: pointer;
|
|
155
|
+
}
|
|
156
|
+
#clr-picker{
|
|
157
|
+
z-index: 999999
|
|
158
|
+
}
|
|
159
|
+
#uni-theme-panel button{
|
|
160
|
+
margin: 0;
|
|
161
|
+
border-radius: 50%
|
|
162
|
+
}
|
|
163
|
+
.clr-field{
|
|
164
|
+
margin: 5px 0
|
|
165
|
+
}
|
|
166
|
+
.uni-color-group {
|
|
167
|
+
display: flex;
|
|
168
|
+
align-items: center;
|
|
169
|
+
justify-content: space-between;
|
|
170
|
+
margin-bottom: 14px;
|
|
171
|
+
gap: 12px;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
.uni-color-group label {
|
|
175
|
+
font-size: 18px;
|
|
176
|
+
font-weight: 500;
|
|
177
|
+
color: var(--text-color, #333);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.uni-color-input {
|
|
181
|
+
width: 36px;
|
|
182
|
+
height: 36px;
|
|
183
|
+
border-radius: 50%;
|
|
184
|
+
border: 2px solid #ddd;
|
|
185
|
+
cursor: pointer;
|
|
186
|
+
padding: 0;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
`;
|
|
190
|
+
document.head.appendChild(style);
|
|
191
|
+
}
|
|
192
|
+
function setTheme(name, themeProps = {}) {
|
|
193
|
+
const theme = name === "custom" ? { name, properties: themeProps } : themes.find((t) => t.name === name);
|
|
60
194
|
if (!theme) {
|
|
61
195
|
console.warn(`Theme "${name}" not found`);
|
|
62
196
|
return;
|
|
@@ -67,6 +201,61 @@ function setTheme(name) {
|
|
|
67
201
|
});
|
|
68
202
|
localStorage.setItem("uts-theme", name);
|
|
69
203
|
}
|
|
204
|
+
function constructPanel(container) {
|
|
205
|
+
const panel = document.createElement("div");
|
|
206
|
+
panel.id = "uni-theme-panel";
|
|
207
|
+
const title = document.createElement("h3");
|
|
208
|
+
title.textContent = "Select Custom Theme";
|
|
209
|
+
panel.appendChild(title);
|
|
210
|
+
colorFields.forEach((field) => {
|
|
211
|
+
const group = document.createElement("div");
|
|
212
|
+
group.className = "uni-color-group";
|
|
213
|
+
const label = document.createElement("label");
|
|
214
|
+
label.textContent = field.label;
|
|
215
|
+
const input = document.createElement("input");
|
|
216
|
+
input.type = "text";
|
|
217
|
+
input.className = "uni-color-input";
|
|
218
|
+
input.id = `uni-theme-${field.id}`;
|
|
219
|
+
input.value = "#000";
|
|
220
|
+
group.appendChild(label);
|
|
221
|
+
group.appendChild(input);
|
|
222
|
+
panel.appendChild(group);
|
|
223
|
+
});
|
|
224
|
+
container.appendChild(panel);
|
|
225
|
+
}
|
|
226
|
+
function injectHTML(options = {}) {
|
|
227
|
+
var _a, _b;
|
|
228
|
+
const container = document.createElement("div");
|
|
229
|
+
if (document.getElementById("uni-theme-root")) return;
|
|
230
|
+
container.id = "uni-theme-root";
|
|
231
|
+
container.innerHTML = `
|
|
232
|
+
<div id='uni-theme-toggle'>\u{1F3A8}</div>
|
|
233
|
+
`;
|
|
234
|
+
constructPanel(container);
|
|
235
|
+
(_a = document.getElementById(options.containerId)) == null ? void 0 : _a.style.setProperty("position", "relative");
|
|
236
|
+
options.containerId ? (_b = document.getElementById(options.containerId)) == null ? void 0 : _b.append(container) : document.body.appendChild(container);
|
|
237
|
+
return container;
|
|
238
|
+
}
|
|
239
|
+
function initThemeElements() {
|
|
240
|
+
const themeToggler = document.getElementById("uni-theme-toggle");
|
|
241
|
+
const themePanel = document.getElementById("uni-theme-panel");
|
|
242
|
+
import_coloris2.default.init();
|
|
243
|
+
document.querySelectorAll(".uni-color-input").forEach((input) => {
|
|
244
|
+
console.log(input.className);
|
|
245
|
+
(0, import_coloris2.default)({
|
|
246
|
+
el: `.${input.className}`,
|
|
247
|
+
alpha: true,
|
|
248
|
+
format: "hex"
|
|
249
|
+
});
|
|
250
|
+
input.addEventListener("input", (e) => {
|
|
251
|
+
const target = e.target;
|
|
252
|
+
target.style.background = target.value;
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
return { themeToggler, themePanel };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// src/themeManager.ts
|
|
70
259
|
function getAvailableThemes() {
|
|
71
260
|
return themes.map((t) => t.name);
|
|
72
261
|
}
|
|
@@ -76,10 +265,23 @@ function loadSavedTheme() {
|
|
|
76
265
|
setTheme(saved);
|
|
77
266
|
}
|
|
78
267
|
}
|
|
268
|
+
function mountToggleTheme(options = {}) {
|
|
269
|
+
injectStyles();
|
|
270
|
+
const container = injectHTML(options);
|
|
271
|
+
const { themeToggler, themePanel } = initThemeElements();
|
|
272
|
+
themeToggler == null ? void 0 : themeToggler.addEventListener("click", () => {
|
|
273
|
+
container == null ? void 0 : container.classList.toggle("opened");
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
function unMountToggleTheme() {
|
|
277
|
+
var _a;
|
|
278
|
+
(_a = document.getElementById("uni-theme-root")) == null ? void 0 : _a.remove();
|
|
279
|
+
}
|
|
79
280
|
// Annotate the CommonJS export names for ESM import in node:
|
|
80
281
|
0 && (module.exports = {
|
|
81
282
|
getAvailableThemes,
|
|
82
283
|
loadSavedTheme,
|
|
83
|
-
|
|
84
|
-
themes
|
|
284
|
+
mountToggleTheme,
|
|
285
|
+
themes,
|
|
286
|
+
unMountToggleTheme
|
|
85
287
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -23,12 +23,134 @@ var themes = [
|
|
|
23
23
|
"text-color": "#333333",
|
|
24
24
|
"primary-color": "#c19a6b"
|
|
25
25
|
}
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: "custom",
|
|
29
|
+
properties: {
|
|
30
|
+
"bg-color": "",
|
|
31
|
+
"text-color": "",
|
|
32
|
+
"primary-color": ""
|
|
33
|
+
}
|
|
26
34
|
}
|
|
27
35
|
];
|
|
28
36
|
|
|
29
|
-
// src/
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
// src/coreFunctions.ts
|
|
38
|
+
import "@melloware/coloris/dist/coloris.css";
|
|
39
|
+
import Coloris from "@melloware/coloris";
|
|
40
|
+
var colorFields = [
|
|
41
|
+
{ id: "bgColor", label: "Background" },
|
|
42
|
+
{ id: "primaryColor", label: "Primary" },
|
|
43
|
+
{ id: "secondaryColor", label: "Secondary" },
|
|
44
|
+
{ id: "borderColor", label: "Border Color" },
|
|
45
|
+
{ id: "textColor", label: "Text Color" }
|
|
46
|
+
];
|
|
47
|
+
function injectStyles() {
|
|
48
|
+
if (document.getElementById("uni-theme-style")) return;
|
|
49
|
+
const style = document.createElement("style");
|
|
50
|
+
style.id = "uni-theme-style";
|
|
51
|
+
style.innerHTML = `
|
|
52
|
+
#uni-theme-toggle {
|
|
53
|
+
position: fixed;
|
|
54
|
+
bottom: 20px;
|
|
55
|
+
right: 20px;
|
|
56
|
+
width: 50px;
|
|
57
|
+
height: 50px;
|
|
58
|
+
background: var(--bg, #222);
|
|
59
|
+
color: var(--text, #fff);
|
|
60
|
+
display: flex;
|
|
61
|
+
align-items: center;
|
|
62
|
+
justify-content: center;
|
|
63
|
+
border-radius: 50%;
|
|
64
|
+
cursor: pointer;
|
|
65
|
+
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
|
|
66
|
+
z-index: 9999;
|
|
67
|
+
font-size:20px;
|
|
68
|
+
transition: right 0.3s ease;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#uni-theme-overlay {
|
|
72
|
+
position: fixed;
|
|
73
|
+
inset: 0;
|
|
74
|
+
background: rgba(0,0,0,0.3);
|
|
75
|
+
opacity: 0;
|
|
76
|
+
pointer-events: none;
|
|
77
|
+
transition: opacity 0.3s ease;
|
|
78
|
+
z-index: 9998;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
#uni-theme-overlay.show {
|
|
82
|
+
opacity: 1;
|
|
83
|
+
pointer-events: all;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
#uni-theme-panel {
|
|
87
|
+
position: fixed;
|
|
88
|
+
bottom: 20px;
|
|
89
|
+
right: -322px;
|
|
90
|
+
width: 280px;
|
|
91
|
+
background: var(--bg, #fff);
|
|
92
|
+
color: var(--text, #000);
|
|
93
|
+
padding: 20px;
|
|
94
|
+
box-shadow: -4px 0 12px rgba(0,0,0,0.2);
|
|
95
|
+
transition: right 0.3s ease;
|
|
96
|
+
z-index: 9999;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#uni-theme-root.opened #uni-theme-panel{
|
|
100
|
+
right: 0;
|
|
101
|
+
max-height: 500px;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
#uni-theme-root.opened #uni-theme-toggle{
|
|
105
|
+
right: 322px;
|
|
106
|
+
transition: left 0.3s ease;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#uni-theme-panel button {
|
|
110
|
+
display: block;
|
|
111
|
+
margin: 10px 0;
|
|
112
|
+
padding: 8px;
|
|
113
|
+
width: 100%;
|
|
114
|
+
cursor: pointer;
|
|
115
|
+
}
|
|
116
|
+
#clr-picker{
|
|
117
|
+
z-index: 999999
|
|
118
|
+
}
|
|
119
|
+
#uni-theme-panel button{
|
|
120
|
+
margin: 0;
|
|
121
|
+
border-radius: 50%
|
|
122
|
+
}
|
|
123
|
+
.clr-field{
|
|
124
|
+
margin: 5px 0
|
|
125
|
+
}
|
|
126
|
+
.uni-color-group {
|
|
127
|
+
display: flex;
|
|
128
|
+
align-items: center;
|
|
129
|
+
justify-content: space-between;
|
|
130
|
+
margin-bottom: 14px;
|
|
131
|
+
gap: 12px;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.uni-color-group label {
|
|
135
|
+
font-size: 18px;
|
|
136
|
+
font-weight: 500;
|
|
137
|
+
color: var(--text-color, #333);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.uni-color-input {
|
|
141
|
+
width: 36px;
|
|
142
|
+
height: 36px;
|
|
143
|
+
border-radius: 50%;
|
|
144
|
+
border: 2px solid #ddd;
|
|
145
|
+
cursor: pointer;
|
|
146
|
+
padding: 0;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
`;
|
|
150
|
+
document.head.appendChild(style);
|
|
151
|
+
}
|
|
152
|
+
function setTheme(name, themeProps = {}) {
|
|
153
|
+
const theme = name === "custom" ? { name, properties: themeProps } : themes.find((t) => t.name === name);
|
|
32
154
|
if (!theme) {
|
|
33
155
|
console.warn(`Theme "${name}" not found`);
|
|
34
156
|
return;
|
|
@@ -39,6 +161,61 @@ function setTheme(name) {
|
|
|
39
161
|
});
|
|
40
162
|
localStorage.setItem("uts-theme", name);
|
|
41
163
|
}
|
|
164
|
+
function constructPanel(container) {
|
|
165
|
+
const panel = document.createElement("div");
|
|
166
|
+
panel.id = "uni-theme-panel";
|
|
167
|
+
const title = document.createElement("h3");
|
|
168
|
+
title.textContent = "Select Custom Theme";
|
|
169
|
+
panel.appendChild(title);
|
|
170
|
+
colorFields.forEach((field) => {
|
|
171
|
+
const group = document.createElement("div");
|
|
172
|
+
group.className = "uni-color-group";
|
|
173
|
+
const label = document.createElement("label");
|
|
174
|
+
label.textContent = field.label;
|
|
175
|
+
const input = document.createElement("input");
|
|
176
|
+
input.type = "text";
|
|
177
|
+
input.className = "uni-color-input";
|
|
178
|
+
input.id = `uni-theme-${field.id}`;
|
|
179
|
+
input.value = "#000";
|
|
180
|
+
group.appendChild(label);
|
|
181
|
+
group.appendChild(input);
|
|
182
|
+
panel.appendChild(group);
|
|
183
|
+
});
|
|
184
|
+
container.appendChild(panel);
|
|
185
|
+
}
|
|
186
|
+
function injectHTML(options = {}) {
|
|
187
|
+
var _a, _b;
|
|
188
|
+
const container = document.createElement("div");
|
|
189
|
+
if (document.getElementById("uni-theme-root")) return;
|
|
190
|
+
container.id = "uni-theme-root";
|
|
191
|
+
container.innerHTML = `
|
|
192
|
+
<div id='uni-theme-toggle'>\u{1F3A8}</div>
|
|
193
|
+
`;
|
|
194
|
+
constructPanel(container);
|
|
195
|
+
(_a = document.getElementById(options.containerId)) == null ? void 0 : _a.style.setProperty("position", "relative");
|
|
196
|
+
options.containerId ? (_b = document.getElementById(options.containerId)) == null ? void 0 : _b.append(container) : document.body.appendChild(container);
|
|
197
|
+
return container;
|
|
198
|
+
}
|
|
199
|
+
function initThemeElements() {
|
|
200
|
+
const themeToggler = document.getElementById("uni-theme-toggle");
|
|
201
|
+
const themePanel = document.getElementById("uni-theme-panel");
|
|
202
|
+
Coloris.init();
|
|
203
|
+
document.querySelectorAll(".uni-color-input").forEach((input) => {
|
|
204
|
+
console.log(input.className);
|
|
205
|
+
Coloris({
|
|
206
|
+
el: `.${input.className}`,
|
|
207
|
+
alpha: true,
|
|
208
|
+
format: "hex"
|
|
209
|
+
});
|
|
210
|
+
input.addEventListener("input", (e) => {
|
|
211
|
+
const target = e.target;
|
|
212
|
+
target.style.background = target.value;
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
return { themeToggler, themePanel };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/themeManager.ts
|
|
42
219
|
function getAvailableThemes() {
|
|
43
220
|
return themes.map((t) => t.name);
|
|
44
221
|
}
|
|
@@ -48,9 +225,22 @@ function loadSavedTheme() {
|
|
|
48
225
|
setTheme(saved);
|
|
49
226
|
}
|
|
50
227
|
}
|
|
228
|
+
function mountToggleTheme(options = {}) {
|
|
229
|
+
injectStyles();
|
|
230
|
+
const container = injectHTML(options);
|
|
231
|
+
const { themeToggler, themePanel } = initThemeElements();
|
|
232
|
+
themeToggler == null ? void 0 : themeToggler.addEventListener("click", () => {
|
|
233
|
+
container == null ? void 0 : container.classList.toggle("opened");
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
function unMountToggleTheme() {
|
|
237
|
+
var _a;
|
|
238
|
+
(_a = document.getElementById("uni-theme-root")) == null ? void 0 : _a.remove();
|
|
239
|
+
}
|
|
51
240
|
export {
|
|
52
241
|
getAvailableThemes,
|
|
53
242
|
loadSavedTheme,
|
|
54
|
-
|
|
55
|
-
themes
|
|
243
|
+
mountToggleTheme,
|
|
244
|
+
themes,
|
|
245
|
+
unMountToggleTheme
|
|
56
246
|
};
|
package/package.json
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uni-theme-select",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.01",
|
|
4
4
|
"description": "Framework-agnostic theme switcher",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
"files": [
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
9
11
|
"scripts": {
|
|
10
12
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
11
13
|
"dev": "tsup src/index.ts --format cjs,esm --watch --dts"
|
|
12
14
|
},
|
|
13
|
-
"keywords": [
|
|
15
|
+
"keywords": [
|
|
16
|
+
"theme",
|
|
17
|
+
"dark-mode",
|
|
18
|
+
"css-variables"
|
|
19
|
+
],
|
|
14
20
|
"author": "sri_manohari",
|
|
15
21
|
"license": "ISC",
|
|
16
22
|
"devDependencies": {
|
|
17
23
|
"tsup": "^8.5.1",
|
|
18
24
|
"typescript": "^5.9.3"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@melloware/coloris": "^0.25.0"
|
|
19
28
|
}
|
|
20
29
|
}
|