UNPKG

@asphalt-react/helper

Version:

Common helper utilities for components

62 lines (47 loc) 4.7 kB
import { tabbable } from 'tabbable'; const randomHash=()=>`${Math.random().toString(36).substring(2,15)}`;const paddedHexString=buffer=>{const byteArray=new Uint8Array(buffer);const hexCodes=Array.from(byteArray).map(value=>value.toString(16).padStart(2,"0"));return hexCodes.join("")};const sha256=async input=>{{const{createHash}=require("crypto");const hash=createHash("sha256");hash.update(input);return hash.digest("hex")}};const hash=data=>{const encoder=new TextEncoder("utf-8");return sha256(encoder.encode(data))}; var hash$1 = /*#__PURE__*/Object.freeze({ __proto__: null, randomHash: randomHash, paddedHexString: paddedHexString, sha256: sha256, hash: hash }); const isNode=()=>typeof process!=="undefined"; var env = /*#__PURE__*/Object.freeze({ __proto__: null, isNode: isNode }); const INFO="info";const SUCCESS="success";const WARNING="warning";const DANGER="danger";const NEUTRAL="neutral";const INVALID="invalid";const BRAND="brand";const KEYCODES=Object.freeze({TAB:9,RETURN:13,ESC:27,SPACE:32,PAGEUP:33,PAGEDOWN:34,END:35,HOME:36,LEFT:37,UP:38,RIGHT:39,DOWN:40});const ID_PREFIX="sphlt-"; const getPrefixedId=id=>id?`${ID_PREFIX}${id}`:"";const capitalise=(word="")=>word.substring(0,1).toUpperCase()+word.substring(1).toLowerCase();const camelCase=(...words)=>{return words.reduce((result,word,index)=>{word=word.toLowerCase();return result+(index?capitalise(word):word)},"")}; var strings = /*#__PURE__*/Object.freeze({ __proto__: null, getPrefixedId: getPrefixedId, capitalise: capitalise, camelCase: camelCase }); const validateSize=(sizeArr,size)=>{return sizeArr.includes(size)};const createPropOccurValidator=propsEnum=>props=>{const incomingProps=Object.keys(props).filter(key=>Boolean(props[key]));return propsEnum.filter(prop=>incomingProps.includes(prop)).length<=1};const resolvePropCollision=(propsObj,defaultProp)=>{const validateProp=createPropOccurValidator(Object.keys(propsObj));const singleOccur=validateProp({...propsObj});if(!singleOccur){return {collision:true,value:defaultProp}}const truthyProp=Object.keys(propsObj).find(prop=>propsObj[prop]);return {collision:false,value:truthyProp?truthyProp:defaultProp}};const getSupportType=defaultType=>typeProps=>{const{[defaultType]:removedProp,...restProps}=typeProps;const supportKeys=Object.keys(restProps);const doPropsConflict=!createPropOccurValidator(supportKeys)(restProps);if(doPropsConflict){return defaultType}return supportKeys.find(key=>Boolean(restProps[key]))||defaultType};const mergeRefs=(...refs)=>node=>{refs.forEach(ref=>{if(typeof ref==="function"){ref(node);}else if(ref!==null){ref.current=node;}});};const yieldPropsWithPrefix=(obj,prefix)=>Object.keys(obj).reduce((prefixedProps,key)=>({...prefixedProps,...(key.startsWith(prefix)&&{[key]:obj[key]})}),{});const filterPropsWithPrefix=(obj,prefix)=>Object.keys(obj).reduce((prefixedProps,key)=>({...prefixedProps,...(!key.startsWith(prefix)&&{[key]:obj[key]})}),{});const yieldDataAttrs=obj=>yieldPropsWithPrefix(obj,"data-");const excludeDataAttrs=obj=>filterPropsWithPrefix(obj,"data-");const yieldAriaAttrs=obj=>yieldPropsWithPrefix(obj,"aria-");const excludeAriaAttrs=obj=>filterPropsWithPrefix(obj,"aria-");const concatClasses=(...words)=>camelCase(...words); var props = /*#__PURE__*/Object.freeze({ __proto__: null, validateSize: validateSize, createPropOccurValidator: createPropOccurValidator, resolvePropCollision: resolvePropCollision, getSupportType: getSupportType, mergeRefs: mergeRefs, yieldDataAttrs: yieldDataAttrs, excludeDataAttrs: excludeDataAttrs, yieldAriaAttrs: yieldAriaAttrs, excludeAriaAttrs: excludeAriaAttrs, concatClasses: concatClasses }); const isTypeSVG=val=>{const type=typeof val==="function"?val().type:val;return type==="svg"};const isSVG=icon=>{if(!icon)return false;const type=icon.props&&icon.props.originalType||icon.type;return isTypeSVG(type)}; var svg = /*#__PURE__*/Object.freeze({ __proto__: null, isSVG: isSVG }); const firstAndlastFocusEls=element=>{const focusEls=tabbable(element);const total=focusEls.length;return {firstEl:focusEls[0],lastEl:focusEls[total-1],total}};const focusTrap=(element,event)=>{const{firstEl,lastEl}=firstAndlastFocusEls(element);if(event.shiftKey&&document.activeElement===firstEl){lastEl.focus();event.preventDefault();}if(!event.shiftKey&&document.activeElement===lastEl){firstEl.focus();event.preventDefault();}}; var focus = /*#__PURE__*/Object.freeze({ __proto__: null, focusTrap: focusTrap }); export { BRAND, DANGER, INFO, INVALID, KEYCODES, NEUTRAL, SUCCESS, WARNING, env as envUtil, focus as focusUtil, hash$1 as hashUtil, props as propsUtil, strings as stringUtil, svg as svgUtil };