@snowztech/ui-js 0.2.0 → 0.3.0

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/dist/index.cjs CHANGED
@@ -54,8 +54,8 @@ async function copyValue(value) {
54
54
  }
55
55
  }
56
56
  function onClick() {
57
- const value = this.getAttribute("data-sn-copy");
58
- if (value === null) return;
57
+ const value = this.getAttribute("data-sn-copy") || this.closest(".sn-code")?.querySelector("pre")?.textContent;
58
+ if (!value) return;
59
59
  void copyValue(value).then((ok) => {
60
60
  if (!ok) return;
61
61
  this.setAttribute("data-copied", "true");
@@ -79,7 +79,9 @@ function getTheme(storageKey = DEFAULT_KEY) {
79
79
  if (stored === "light" || stored === "dark") return stored;
80
80
  } catch {
81
81
  }
82
- return document.documentElement.getAttribute("data-theme") === "light" ? "light" : "dark";
82
+ const attr = document.documentElement.getAttribute("data-theme");
83
+ if (attr === "system") return matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
84
+ return attr === "light" ? "light" : "dark";
83
85
  }
84
86
  function setTheme(theme, storageKey = DEFAULT_KEY) {
85
87
  if (theme === "light") document.documentElement.setAttribute("data-theme", "light");
package/dist/index.js CHANGED
@@ -21,8 +21,8 @@ async function copyValue(value) {
21
21
  }
22
22
  }
23
23
  function onClick() {
24
- const value = this.getAttribute("data-sn-copy");
25
- if (value === null) return;
24
+ const value = this.getAttribute("data-sn-copy") || this.closest(".sn-code")?.querySelector("pre")?.textContent;
25
+ if (!value) return;
26
26
  void copyValue(value).then((ok) => {
27
27
  if (!ok) return;
28
28
  this.setAttribute("data-copied", "true");
@@ -46,7 +46,9 @@ function getTheme(storageKey = DEFAULT_KEY) {
46
46
  if (stored === "light" || stored === "dark") return stored;
47
47
  } catch {
48
48
  }
49
- return document.documentElement.getAttribute("data-theme") === "light" ? "light" : "dark";
49
+ const attr = document.documentElement.getAttribute("data-theme");
50
+ if (attr === "system") return matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
51
+ return attr === "light" ? "light" : "dark";
50
52
  }
51
53
  function setTheme(theme, storageKey = DEFAULT_KEY) {
52
54
  if (theme === "light") document.documentElement.setAttribute("data-theme", "light");
@@ -21,8 +21,8 @@ async function copyValue(value) {
21
21
  }
22
22
  }
23
23
  function onClick() {
24
- const value = this.getAttribute("data-sn-copy");
25
- if (value === null) return;
24
+ const value = this.getAttribute("data-sn-copy") || this.closest(".sn-code")?.querySelector("pre")?.textContent;
25
+ if (!value) return;
26
26
  void copyValue(value).then((ok) => {
27
27
  if (!ok) return;
28
28
  this.setAttribute("data-copied", "true");
@@ -46,7 +46,9 @@ function getTheme(storageKey = DEFAULT_KEY) {
46
46
  if (stored === "light" || stored === "dark") return stored;
47
47
  } catch {
48
48
  }
49
- return document.documentElement.getAttribute("data-theme") === "light" ? "light" : "dark";
49
+ const attr = document.documentElement.getAttribute("data-theme");
50
+ if (attr === "system") return matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
51
+ return attr === "light" ? "light" : "dark";
50
52
  }
51
53
  function setTheme(theme, storageKey = DEFAULT_KEY) {
52
54
  if (theme === "light") document.documentElement.setAttribute("data-theme", "light");
@@ -47,8 +47,8 @@ var SnowztechUI = (() => {
47
47
  }
48
48
  }
49
49
  function onClick() {
50
- const value = this.getAttribute("data-sn-copy");
51
- if (value === null) return;
50
+ const value = this.getAttribute("data-sn-copy") || this.closest(".sn-code")?.querySelector("pre")?.textContent;
51
+ if (!value) return;
52
52
  void copyValue(value).then((ok) => {
53
53
  if (!ok) return;
54
54
  this.setAttribute("data-copied", "true");
@@ -72,7 +72,9 @@ var SnowztechUI = (() => {
72
72
  if (stored === "light" || stored === "dark") return stored;
73
73
  } catch {
74
74
  }
75
- return document.documentElement.getAttribute("data-theme") === "light" ? "light" : "dark";
75
+ const attr = document.documentElement.getAttribute("data-theme");
76
+ if (attr === "system") return matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
77
+ return attr === "light" ? "light" : "dark";
76
78
  }
77
79
  function setTheme(theme, storageKey = DEFAULT_KEY) {
78
80
  if (theme === "light") document.documentElement.setAttribute("data-theme", "light");
@@ -1 +1 @@
1
- "use strict";var SnowztechUI=(()=>{var r=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var s=Object.prototype.hasOwnProperty;var h=(t,e)=>{for(var n in e)r(t,n,{get:e[n],enumerable:!0})},p=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of l(e))!s.call(t,o)&&o!==n&&r(t,o,{get:()=>e[o],enumerable:!(a=m(e,o))||a.enumerable});return t};var f=t=>p(r({},"__esModule",{value:!0}),t);var L={};h(L,{init:()=>d});async function g(t){try{return await navigator.clipboard.writeText(t),!0}catch{let e=document.createElement("textarea");e.value=t,e.style.position="fixed",e.style.opacity="0",document.body.appendChild(e),e.select();let n=!1;try{n=document.execCommand("copy")}catch{}return document.body.removeChild(e),n}}function T(){let t=this.getAttribute("data-sn-copy");t!==null&&g(t).then(e=>{e&&(this.setAttribute("data-copied","true"),window.setTimeout(()=>this.removeAttribute("data-copied"),1200))})}function y(t){t.dataset.snBound!=="copy"&&(t.dataset.snBound="copy",t.addEventListener("click",T))}function c(t=document){t.querySelectorAll("[data-sn-copy]").forEach(y)}var i="theme";function E(t=i){try{let e=localStorage.getItem(t);if(e==="light"||e==="dark")return e}catch{}return document.documentElement.getAttribute("data-theme")==="light"?"light":"dark"}function v(t,e=i){t==="light"?document.documentElement.setAttribute("data-theme","light"):document.documentElement.removeAttribute("data-theme");try{localStorage.setItem(e,t)}catch{}}function x(t=i){let e=E(t)==="dark"?"light":"dark";return v(e,t),e}function b(){let t=this.getAttribute("data-sn-storage-key")||i;x(t)}function k(t){t.dataset.snBound!=="theme"&&(t.dataset.snBound="theme",t.addEventListener("click",b))}function u(t=document){t.querySelectorAll("[data-sn-theme-toggle]").forEach(k)}function d(t=document){c(t),u(t)}typeof document<"u"&&(document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>d(),{once:!0}):d());return f(L);})();
1
+ "use strict";var SnowztechUI=(()=>{var r=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var s=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var h=(t,e)=>{for(var n in e)r(t,n,{get:e[n],enumerable:!0})},p=(t,e,n,d)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of s(e))!l.call(t,o)&&o!==n&&r(t,o,{get:()=>e[o],enumerable:!(d=m(e,o))||d.enumerable});return t};var f=t=>p(r({},"__esModule",{value:!0}),t);var L={};h(L,{init:()=>c});async function g(t){try{return await navigator.clipboard.writeText(t),!0}catch{let e=document.createElement("textarea");e.value=t,e.style.position="fixed",e.style.opacity="0",document.body.appendChild(e),e.select();let n=!1;try{n=document.execCommand("copy")}catch{}return document.body.removeChild(e),n}}function y(){let t=this.getAttribute("data-sn-copy")||this.closest(".sn-code")?.querySelector("pre")?.textContent;t&&g(t).then(e=>{e&&(this.setAttribute("data-copied","true"),window.setTimeout(()=>this.removeAttribute("data-copied"),1200))})}function T(t){t.dataset.snBound!=="copy"&&(t.dataset.snBound="copy",t.addEventListener("click",y))}function a(t=document){t.querySelectorAll("[data-sn-copy]").forEach(T)}var i="theme";function E(t=i){try{let n=localStorage.getItem(t);if(n==="light"||n==="dark")return n}catch{}let e=document.documentElement.getAttribute("data-theme");return e==="system"?matchMedia("(prefers-color-scheme: light)").matches?"light":"dark":e==="light"?"light":"dark"}function v(t,e=i){t==="light"?document.documentElement.setAttribute("data-theme","light"):document.documentElement.removeAttribute("data-theme");try{localStorage.setItem(e,t)}catch{}}function x(t=i){let e=E(t)==="dark"?"light":"dark";return v(e,t),e}function b(){let t=this.getAttribute("data-sn-storage-key")||i;x(t)}function k(t){t.dataset.snBound!=="theme"&&(t.dataset.snBound="theme",t.addEventListener("click",b))}function u(t=document){t.querySelectorAll("[data-sn-theme-toggle]").forEach(k)}function c(t=document){a(t),u(t)}typeof document<"u"&&(document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>c(),{once:!0}):c());return f(L);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowztech/ui-js",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Optional vanilla bindings for @snowztech/ui-css. Wires data-sn-* attributes for static HTML.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -1,4 +1,5 @@
1
- // Wires every <button data-sn-copy="…"> on the page.
1
+ // Wires every <button data-sn-copy="…"> on the page. Leave the value empty
2
+ // inside a .sn-code block to copy that block's code.
2
3
  // On click: copies the attr's value, flips data-copied="true" for ~1.2s.
3
4
  // CSS handles the visual swap (.sn-copy-btn[data-copied="true"]).
4
5
 
@@ -24,8 +25,11 @@ async function copyValue(value: string): Promise<boolean> {
24
25
  }
25
26
 
26
27
  function onClick(this: HTMLElement) {
27
- const value = this.getAttribute("data-sn-copy");
28
- if (value === null) return;
28
+ // Empty attribute inside a .sn-code block copies the block's own code.
29
+ const value =
30
+ this.getAttribute("data-sn-copy") ||
31
+ this.closest(".sn-code")?.querySelector("pre")?.textContent;
32
+ if (!value) return;
29
33
  void copyValue(value).then((ok) => {
30
34
  if (!ok) return;
31
35
  this.setAttribute("data-copied", "true");
@@ -10,7 +10,9 @@ export function getTheme(storageKey = DEFAULT_KEY): Theme {
10
10
  const stored = localStorage.getItem(storageKey) as Theme | null;
11
11
  if (stored === "light" || stored === "dark") return stored;
12
12
  } catch { /* ignore */ }
13
- return document.documentElement.getAttribute("data-theme") === "light" ? "light" : "dark";
13
+ const attr = document.documentElement.getAttribute("data-theme");
14
+ if (attr === "system") return matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
15
+ return attr === "light" ? "light" : "dark";
14
16
  }
15
17
 
16
18
  export function setTheme(theme: Theme, storageKey = DEFAULT_KEY): void {