UNPKG

tdesign-react

Version:
101 lines (96 loc) 4.59 kB
/** * tdesign v1.16.2 * (c) 2025 tdesign * @license MIT */ import { _ as _slicedToArray } from '../_chunks/dep-10d5731f.js'; import { useState, useMemo } from 'react'; import { T as THEME_MODE } from '../_chunks/dep-9fcdea87.js'; import useMutationObservable from './useMutationObserver.js'; import { c as canUseDocument } from '../_chunks/dep-ca8d3fa0.js'; import '../_chunks/dep-74a10cfb.js'; import '../_chunks/dep-d67deb2c.js'; import '../_chunks/dep-8abcbcbc.js'; import 'lodash-es'; import './useLatest.js'; var getColorTokenColor = function getColorTokenColor(token) { var _document, _styles$getPropertyVa; if (typeof window === "undefined") return ""; var targetElement = (_document = document) === null || _document === void 0 ? void 0 : _document.documentElement; var styles = getComputedStyle(targetElement); return (_styles$getPropertyVa = styles.getPropertyValue(token).trim()) !== null && _styles$getPropertyVa !== void 0 ? _styles$getPropertyVa : ""; }; function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var DEFAULT_OPTIONS = { debounceTime: 250, config: { attributes: true } }; function useVariables(variables, targetElement) { var _useState = useState({}), _useState2 = _slicedToArray(_useState, 2), forceUpdate = _useState2[1]; if (canUseDocument && !targetElement) { var _document; targetElement = (_document = document) === null || _document === void 0 ? void 0 : _document.documentElement; } if (!variables || Object.keys(variables).length === 0) { throw new Error("useVariables: variables parameter cannot be empty"); } var refs = useMemo(function () { var values = {}; Object.entries(variables).forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), key = _ref2[0], varName = _ref2[1]; try { var initialValue = getColorTokenColor(varName); values[key] = initialValue; } catch (error) { console.warn("Failed to get initial value for CSS variable ".concat(varName, ":"), error); values[key] = ""; } }); return values; }, [variables]); var updateVariables = function updateVariables() { try { Object.entries(variables).forEach(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 2), key = _ref4[0], varName = _ref4[1]; var newValue = getColorTokenColor(varName); if (refs[key] && refs[key] !== newValue) { refs[key] = newValue; } }); forceUpdate({}); } catch (error) { console.warn("Failed to update CSS variables:", error); } }; useMutationObservable(targetElement, function (mutationsList) { var _iterator = _createForOfIteratorHelper(mutationsList), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var mutation = _step.value; if (mutation.type === "attributes" && mutation.attributeName === THEME_MODE) { updateVariables(); return; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } }, DEFAULT_OPTIONS); if (!canUseDocument) return {}; return refs; } export { useVariables as default }; //# sourceMappingURL=useVariables.js.map