@baseline-ui/utils
Advanced tools
Comparing version 0.0.0-nightly-20241024000614 to 0.0.0-nightly-20241026000612
@@ -69,8 +69,9 @@ import React from 'react'; | ||
* | ||
* @param text - The text to measure. | ||
* @param fontSize - The font size in pixels. | ||
* @param fontFamily - The font family. Defaults to Arial. | ||
* @param fontWeight - The font weight. Defaults to normal. | ||
* @param fontStyle - The font style. Defaults to normal. | ||
* @param font - The full font string. Overrides fontSize, fontFamily, | ||
* @param options - The options for calculating the text dimensions. | ||
* @param options.text - The text to measure. | ||
* @param options.fontSize - The font size in pixels. Defaults to 16. | ||
* @param options.fontFamily - The font family. Defaults to 'Arial'. | ||
* @param options.fontWeight - The font weight. Defaults to 'normal'. | ||
* @param options.fontStyle - The font style. Defaults to 'normal'. | ||
* @param options.font - The full font string. Overrides fontSize, fontFamily, | ||
* fontWeight, and fontStyle. | ||
@@ -89,19 +90,21 @@ */ | ||
* | ||
* ```ts | ||
* const fontSize = calculateFontSizeToFitWidth({ | ||
* text: "Hello world!", | ||
* maxWidth: 100, | ||
* initialFontSize: 16, | ||
* fontFamily: "Arial", | ||
* fontWeight: "normal", | ||
* fontStyle: "normal", | ||
* }); | ||
* ``` | ||
* @example | ||
* const fontSize = calculateFontSizeToFitWidth({ | ||
* text: "Hello world!", | ||
* maxWidth: 100, | ||
* initialFontSize: 16, | ||
* fontFamily: "Arial", | ||
* fontWeight: "normal", | ||
* fontStyle: "normal", | ||
* }); | ||
* | ||
* @param text - The text to measure. | ||
* @param maxWidth - The maximum width in pixels. | ||
* @param initialFontSize - The initial font size in pixels. Defaults to 50. | ||
* @param fontFamily - The font family. Defaults to Arial. | ||
* @param fontWeight - The font weight. Defaults to normal. | ||
* @param fontStyle - The font style. Defaults to normal. | ||
* @param options - The options for calculating the font size. | ||
* @param options.text - The text to fit within the width. | ||
* @param options.maxWidth - The maximum width the text should fit within. | ||
* @param options.initialFontSize - The starting font size to use for | ||
* calculations. Defaults to 50. | ||
* @param options.fontFamily - The font family to use. Defaults to 'Arial'. | ||
* @param options.fontWeight - The font weight to use. Defaults to 'normal'. | ||
* @param options.fontStyle - The font style to use. Defaults to 'normal'. | ||
* @returns The calculated font size that fits the text within the given width. | ||
*/ | ||
@@ -113,2 +116,9 @@ declare function calculateFontSizeToFitWidth({ text, maxWidth, initialFontSize, fontFamily, fontWeight, fontStyle, }: { | ||
} & Omit<FontProperties, "fontSize">): number; | ||
/** | ||
* Returns true if the element is focusable. | ||
* | ||
* @param element - The element to check. | ||
*/ | ||
declare function isFocusableElement(element: HTMLElement): boolean; | ||
declare function findFocusableElements(element: HTMLElement): NodeListOf<Element>; | ||
@@ -189,2 +199,2 @@ /** | ||
export { calculateFontSizeToFitWidth, classNames, cleanKeyFromGlobImport, filterTruthyValues, getHTMLElement, getOsSpecificKeyboardShortcutLabel, getPlainText, getSvgPathFromStroke, getTextDimensions, invariant, isUrl, lightenColor }; | ||
export { calculateFontSizeToFitWidth, classNames, cleanKeyFromGlobImport, filterTruthyValues, findFocusableElements, getHTMLElement, getOsSpecificKeyboardShortcutLabel, getPlainText, getSvgPathFromStroke, getTextDimensions, invariant, isFocusableElement, isUrl, lightenColor }; |
'use strict'; | ||
var color = require('@react-stately/color'); | ||
var g = require('dompurify'); | ||
var h = require('dompurify'); | ||
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } | ||
var g__default = /*#__PURE__*/_interopDefault(g); | ||
var h__default = /*#__PURE__*/_interopDefault(h); | ||
@@ -19,15 +19,17 @@ /** | ||
*/ | ||
function d(e,t){if(!e)throw new Error(t||"Invariant failed")}function u(...e){let t=e.filter(Boolean),o=[];for(let r of t)if(Array.isArray(r))o.push(u(...r));else if(typeof r=="object"){let a=Object.keys(r);for(let n of a)r[n]&&o.push(n);}else typeof r=="string"&&o.push(r);return o.join(" ")}function v(e,t){if(!e)return null;if(typeof e=="string")return t.querySelector(e);if(e instanceof HTMLElement)return e;if("current"in e)return e.current}function p({text:e,fontSize:t=16,fontFamily:o="Arial",fontWeight:r="normal",fontStyle:a="normal",font:n}){let s=document.createElement("canvas").getContext("2d");if(!s)return {width:0,height:0};s.font=n||`${a} ${r} ${t}px ${o}`;let i=s.measureText(e);return {width:i.width,height:i.actualBoundingBoxAscent+i.actualBoundingBoxDescent}}function P({text:e,maxWidth:t,initialFontSize:o=50,fontFamily:r="Arial",fontWeight:a="normal",fontStyle:n="normal"}){let{width:c}=p({text:e,fontSize:o,fontFamily:r,fontWeight:a,fontStyle:n}),s=o,i=c;for(;i>t;)s-=.5,i=p({text:e,fontSize:s,fontFamily:r,fontWeight:a,fontStyle:n}).width;return Math.max(s,1)}var f=(e,t)=>(e+t)/2;function E(e,t=!0){if(!e.length)return "";let o=e.length;if(o<4)return "";let r=e[0],a=e[1],n=e[2],c=`M${r[0].toFixed(2)},${r[1].toFixed(2)} Q${a[0].toFixed(2)},${a[1].toFixed(2)} ${f(a[0],n[0]).toFixed(2)},${f(a[1],n[1]).toFixed(2)} T`;for(let s=2,i=o-1;s<i;s++)r=e[s],a=e[s+1],c+=`${f(r[0],a[0]).toFixed(2)},${f(r[1],a[1]).toFixed(2)} `;return t&&(c+="Z"),c}function b(e){return e.split("/").pop()?.split(".").shift()}function D(e){let t={};for(let o of Object.keys(e)){let r=b(o)??"";t[r]=e[o];}return t}function y(e){return Object.fromEntries(Object.entries(e).filter(([,t])=>!!t))}function T(e,t){d(t>=0&&t<=100,"Percent must be between 0 and 100");let o=color.parseColor(e),r=o.getChannelValue("red"),a=o.getChannelValue("green"),n=o.getChannelValue("blue"),c=Math.round(r+(255-r)*t/100),s=Math.round(a+(255-a)*t/100),i=Math.round(n+(255-n)*t/100);return `#${c.toString(16)}${s.toString(16)}${i.toString(16)}`}function m(e,t){if(e=e.trim(),e.includes(" "))return !1;try{return new URL(e),!0}catch{return t?m(`https://${e}`):!1}}function x(e){var t;return typeof window<"u"&&window.navigator!=null?e.test(((t=window.navigator.userAgentData)===null||t===void 0?void 0:t.platform)||window.navigator.platform):!1}function $(){return x(/^Mac/i)}function G(e,t=!1){return $()?e.replace("Ctrl+",t?"\u2318":"Cmd+").replace("Alt+",t?"\u2325":"Option+").replace("Shift+",t?"\u21E7":"Shift+").replace("Enter+",t?"\u21A9":"Enter+").replace("Space+",t?"\u2423":"Space+").replace("Enter",t?"\u21A9":"Enter"):e}var Z=e=>new DOMParser().parseFromString(g__default.default.sanitize(e),"text/html").body.textContent||""; | ||
function $(e,t){if(!e)throw new Error(t||"Invariant failed")}function b(...e){let t=e.filter(Boolean),o=[];for(let r of t)if(Array.isArray(r))o.push(b(...r));else if(typeof r=="object"){let a=Object.keys(r);for(let s of a)r[s]&&o.push(s);}else typeof r=="string"&&o.push(r);return o.join(" ")}function S(e,t){if(!e)return null;if(typeof e=="string")return t.querySelector(e);if(e instanceof HTMLElement)return e;if("current"in e)return e.current}function p({text:e,fontSize:t=16,fontFamily:o="Arial",fontWeight:r="normal",fontStyle:a="normal",font:s}){let n=document.createElement("canvas").getContext("2d");if(!n)return {width:0,height:0};n.font=s||`${a} ${r} ${t}px ${o}`;let i=n.measureText(e);return {width:i.width,height:i.actualBoundingBoxAscent+i.actualBoundingBoxDescent}}function E({text:e,maxWidth:t,initialFontSize:o=50,fontFamily:r="Arial",fontWeight:a="normal",fontStyle:s="normal"}){let{width:c}=p({text:e,fontSize:o,fontFamily:r,fontWeight:a,fontStyle:s}),n=o,i=c;for(;i>t;)n-=.5,i=p({text:e,fontSize:n,fontFamily:r,fontWeight:a,fontStyle:s}).width;return Math.max(n,1)}var u=["a[href]","button:not([disabled])",'input:not([disabled]):not([type="hidden"])',"select:not([disabled])","textarea:not([disabled])",'[tabindex]:not([tabindex="-1"])'];function P(e){return u.some(t=>e.matches(t))}function R(e){return e.querySelectorAll(u.join(", "))}var f=(e,t)=>(e+t)/2;function y(e,t=!0){if(!e.length)return "";let o=e.length;if(o<4)return "";let r=e[0],a=e[1],s=e[2],c=`M${r[0].toFixed(2)},${r[1].toFixed(2)} Q${a[0].toFixed(2)},${a[1].toFixed(2)} ${f(a[0],s[0]).toFixed(2)},${f(a[1],s[1]).toFixed(2)} T`;for(let n=2,i=o-1;n<i;n++)r=e[n],a=e[n+1],c+=`${f(r[0],a[0]).toFixed(2)},${f(r[1],a[1]).toFixed(2)} `;return t&&(c+="Z"),c}function l(e){return e.split("/").pop()?.split(".").shift()}function D(e){let t={};for(let o of Object.keys(e)){let r=l(o)??"";t[r]=e[o];}return t}function T(e){return Object.fromEntries(Object.entries(e).filter(([,t])=>!!t))}function C(e,t){$(t>=0&&t<=100,"Percent must be between 0 and 100");let o=color.parseColor(e),r=o.getChannelValue("red"),a=o.getChannelValue("green"),s=o.getChannelValue("blue"),c=Math.round(r+(255-r)*t/100),n=Math.round(a+(255-a)*t/100),i=Math.round(s+(255-s)*t/100);return `#${c.toString(16)}${n.toString(16)}${i.toString(16)}`}function x(e,t){if(e=e.trim(),e.includes(" "))return !1;try{return new URL(e),!0}catch{return t?x(`https://${e}`):!1}}function g(e){var t;return typeof window<"u"&&window.navigator!=null?e.test(((t=window.navigator.userAgentData)===null||t===void 0?void 0:t.platform)||window.navigator.platform):!1}function d(){return g(/^Mac/i)}function Z(e,t=!1){return d()?e.replace("Ctrl+",t?"\u2318":"Cmd+").replace("Alt+",t?"\u2325":"Option+").replace("Shift+",t?"\u21E7":"Shift+").replace("Enter+",t?"\u21A9":"Enter+").replace("Space+",t?"\u2423":"Space+").replace("Enter",t?"\u21A9":"Enter"):e}var Y=e=>new DOMParser().parseFromString(h__default.default.sanitize(e),"text/html").body.textContent||""; | ||
exports.calculateFontSizeToFitWidth = P; | ||
exports.classNames = u; | ||
exports.calculateFontSizeToFitWidth = E; | ||
exports.classNames = b; | ||
exports.cleanKeyFromGlobImport = D; | ||
exports.filterTruthyValues = y; | ||
exports.getHTMLElement = v; | ||
exports.getOsSpecificKeyboardShortcutLabel = G; | ||
exports.getPlainText = Z; | ||
exports.getSvgPathFromStroke = E; | ||
exports.filterTruthyValues = T; | ||
exports.findFocusableElements = R; | ||
exports.getHTMLElement = S; | ||
exports.getOsSpecificKeyboardShortcutLabel = Z; | ||
exports.getPlainText = Y; | ||
exports.getSvgPathFromStroke = y; | ||
exports.getTextDimensions = p; | ||
exports.invariant = d; | ||
exports.isUrl = m; | ||
exports.lightenColor = T; | ||
exports.invariant = $; | ||
exports.isFocusableElement = P; | ||
exports.isUrl = x; | ||
exports.lightenColor = C; |
{ | ||
"name": "@baseline-ui/utils", | ||
"version": "0.0.0-nightly-20241024000614", | ||
"version": "0.0.0-nightly-20241026000612", | ||
"description": "A collection of utility functions for Baseline UI", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2607454
251