UNPKG

analog-clock-ios-style

Version:

An analog iOS style clock react component with tailwind css

332 lines 14.9 kB
"use strict"; function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_with_holes(arr) { if (Array.isArray(arr)) return arr; } function _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _iterable_to_array_limit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally{ try { if (!_n && _i["return"] != null) _i["return"](); } finally{ if (_d) throw _e; } } return _arr; } function _non_iterable_rest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _object_spread(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === "function") { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function(key) { _define_property(target, key, source[key]); }); } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function(sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _object_spread_props(target, source) { source = source != null ? source : {}; if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function(key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _sliced_to_array(arr, i) { return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); } function _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = function(target, all) { for(var name in all)__defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = function(to, from, except, desc) { if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") { var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { var _loop = function() { var key = _step.value; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: function() { return from[key]; }, enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); }; for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop(); } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } } return to; }; var __toESM = function(mod, isNodeMode, target) { return target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(// If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod); }; var __toCommonJS = function(mod) { return __copyProps(__defProp({}, "__esModule", { value: true }), mod); }; // src/index.ts var index_exports = {}; __export(index_exports, { AnalogClock: function() { return AnalogClock; } }); module.exports = __toCommonJS(index_exports); // src/components/AnalogClock.tsx var import_react = __toESM(require("react")); var AnalogClock = function(param) { var city = param.city, _param_offset = param.offset, offset = _param_offset === void 0 ? 0 : _param_offset, currentTime = param.currentTime, _param_showToday = param.showToday, showToday = _param_showToday === void 0 ? true : _param_showToday, bgColorClassName = param.bgColorClassName, textColorClassName = param.textColorClassName, bgColorHex = param.bgColorHex, textColorHex = param.textColorHex, handColorHex = param.handColorHex, handColorClassName = param.handColorClassName, accentColorHex = param.accentColorHex, accentColorClassName = param.accentColorClassName, _param_size = param.size, size = _param_size === void 0 ? 128 : _param_size; var _ref = _sliced_to_array((0, import_react.useState)(new Date(currentTime)), 2), currentTimeState = _ref[0], setCurrentTimeState = _ref[1]; var animationFrameId = (0, import_react.useRef)(null); var initialTimeRef = (0, import_react.useRef)(new Date(currentTime)); var initialTimestampRef = (0, import_react.useRef)(performance.now()); var _ref1 = _sliced_to_array((0, import_react.useState)(false), 2), isClient = _ref1[0], setIsClient = _ref1[1]; (0, import_react.useEffect)(function() { setIsClient(true); }, []); (0, import_react.useEffect)(function() { initialTimeRef.current = new Date(currentTime); initialTimestampRef.current = performance.now(); setCurrentTimeState(new Date(currentTime)); var updateClock = function() { var elapsedMilliseconds = performance.now() - initialTimestampRef.current; var newDisplayTime = new Date(initialTimeRef.current.getTime() + elapsedMilliseconds); setCurrentTimeState(newDisplayTime); animationFrameId.current = requestAnimationFrame(updateClock); }; animationFrameId.current = requestAnimationFrame(updateClock); return function() { if (animationFrameId.current) { cancelAnimationFrame(animationFrameId.current); } }; }, [ currentTime ]); var getAdjustedTime = function() { var adjustedTime2 = new Date(currentTimeState); if (offset !== 0) { adjustedTime2.setHours(currentTimeState.getHours() + offset); } return adjustedTime2; }; var adjustedTime = getAdjustedTime(); var hours = adjustedTime.getHours(); var minutes = adjustedTime.getMinutes(); var seconds = adjustedTime.getSeconds(); var milliseconds = adjustedTime.getMilliseconds(); var secondDegrees = (seconds + milliseconds / 1e3) / 60 * 360; var minuteDegrees = (minutes + (seconds + milliseconds / 1e3) / 60) / 60 * 360; var hourDegrees = (hours % 12 + (minutes + (seconds + milliseconds / 1e3) / 60) / 60) / 12 * 360; var propHours = currentTime.getHours(); var isDayTime = propHours >= 6 && propHours < 18; var bgColor = bgColorClassName !== null && bgColorClassName !== void 0 ? bgColorClassName : isDayTime ? "bg-white" : "bg-gray-800"; var textColor = textColorClassName !== null && textColorClassName !== void 0 ? textColorClassName : isDayTime ? "text-black" : "text-white"; var effectiveHandColorHex = handColorHex !== null && handColorHex !== void 0 ? handColorHex : isDayTime ? "#000" : "#fff"; var effectiveAccentColorHex = accentColorHex !== null && accentColorHex !== void 0 ? accentColorHex : "#FF9500"; if (!isClient) { return ""; } return /* @__PURE__ */ import_react.default.createElement("div", { className: "flex flex-col items-center", style: { width: "".concat(size * 1.5, "px") } }, /* @__PURE__ */ import_react.default.createElement("div", { "data-testid": "clock-face", className: "relative ".concat(bgColor, " rounded-full flex items-center justify-center shadow-lg mb-2"), style: { width: "".concat(size, "px"), height: "".concat(size, "px") } }, _to_consumable_array(Array(12)).map(function(_, i) { var num = i + 1; var angle = num * 30 - 90; var radian = angle * Math.PI / 180; var x = 50 + 42 * Math.cos(radian); var y = 50 + 42 * Math.sin(radian); return /* @__PURE__ */ import_react.default.createElement("div", { key: num, className: "absolute font-semibold ".concat(textColor), style: { left: "".concat(x, "%"), top: "".concat(y, "%"), transform: "translate(-50%, -50%)", fontSize: "".concat(size * 0.1, "px") } }, num); }), /* @__PURE__ */ import_react.default.createElement("div", { "data-testid": "clock-hand", className: "absolute rounded-full origin-bottom ".concat(handColorClassName !== null && handColorClassName !== void 0 ? handColorClassName : ""), style: _object_spread({ width: "".concat(size * 0.01, "px"), height: "".concat(size * 0.25, "px"), bottom: "50%", transform: "rotate(".concat(hourDegrees, "deg)") }, handColorClassName ? {} : { backgroundColor: effectiveHandColorHex }) }), /* @__PURE__ */ import_react.default.createElement("div", { "data-testid": "clock-hand", className: "absolute rounded-full origin-bottom ".concat(handColorClassName !== null && handColorClassName !== void 0 ? handColorClassName : ""), style: _object_spread({ width: "".concat(size * 0.01, "px"), height: "".concat(size * 0.38, "px"), bottom: "50%", transform: "rotate(".concat(minuteDegrees, "deg)") }, handColorClassName ? {} : { backgroundColor: effectiveHandColorHex }) }), /* @__PURE__ */ import_react.default.createElement("div", { "data-testid": "clock-hand", className: "absolute rounded-full origin-bottom ".concat(accentColorClassName !== null && accentColorClassName !== void 0 ? accentColorClassName : ""), style: _object_spread_props(_object_spread({ width: "".concat(size * 5e-3, "px"), height: "".concat(size * 0.4, "px"), bottom: "50%", transform: "rotate(".concat(secondDegrees, "deg)") }, accentColorClassName ? {} : { backgroundColor: effectiveAccentColorHex }), { transition: "none" }) }), /* @__PURE__ */ import_react.default.createElement("div", { "data-testid": "center-dot", className: "absolute rounded-full ".concat(accentColorClassName !== null && accentColorClassName !== void 0 ? accentColorClassName : ""), style: _object_spread({ width: "".concat(size * 0.02, "px"), height: "".concat(size * 0.02, "px") }, accentColorClassName ? {} : { backgroundColor: effectiveAccentColorHex }) })), /* @__PURE__ */ import_react.default.createElement("div", { className: "text-center" }, city && /* @__PURE__ */ import_react.default.createElement("h3", { className: "text-white font-medium", style: { fontSize: "".concat(size * 0.16, "px") } }, city), showToday && /* @__PURE__ */ import_react.default.createElement("p", { className: "text-gray-400", style: { fontSize: "".concat(size * 0.1, "px") } }, "Today"), offset !== 0 && /* @__PURE__ */ import_react.default.createElement("p", { className: "text-gray-400", style: { fontSize: "".concat(size * 0.1, "px") } }, offset >= 0 ? "+" : "", offset, "HRS"))); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AnalogClock: AnalogClock }); //# sourceMappingURL=index.js.map