@contentful/f36-utils
Advanced tools
Comparing version 4.0.1-next-v4-9789.2434 to 4.0.1-next-v4-9898.2438
128
dist/main.js
@@ -1,2 +0,128 @@ | ||
var e=require("react"),r=d(e),{useEffect:t,useRef:n,useCallback:o,useState:c}=e,{createPortal:u}=require("react-dom"),a=d(require("@babel/runtime/helpers/extends")),{css:s,cx:l}=require("emotion");function i(e,r,t,n){Object.defineProperty(e,r,{get:t,set:n,enumerable:!0,configurable:!0})}function d(e){return e&&e.__esModule?e.default:e}var f={};function p({children:e,container:r}){const o=n(r),c=n(document.createElement("div"));return t((()=>{o.current||(o.current=document.body);const e=c.current;return e.setAttribute("data-cf-ui-portal",""),o.current.appendChild(e),()=>{o.current&&o.current.removeChild(e)}}),[]),c.current?u(e,c.current):null}function y({className:e,children:t,...n}){return r.createElement("span",a({tabIndex:-1,className:l(s({name:"t8ljbi",styles:"display:inherit;outline:0;"}),e)},n),t)}i(f,"Portal",(()=>p)),i(f,"TabFocusTrap",(()=>y)),i(f,"useArrowKeyNavigation",(()=>b)),i(f,"useKeyboard",(()=>v));const m={vertical:{prev:"ArrowUp",next:"ArrowDown"},horizontal:{prev:"ArrowLeft",next:"ArrowRight"}},b=({itemsContainerRef:e,itemsSelector:r,keyType:t="vertical"})=>{const[n,u]=c(0),a=o((o=>{const c=e.current;if(!c)return;const a=c.querySelectorAll(r);if(0===a.length)return;const s=a.length-1,l={[m[t].next]:()=>{u(n===s?0:n+1)},[m[t].prev]:()=>{u(0===n?s:n-1)}}[o.key];l&&(o.preventDefault(),l())}),[n,r,e,t]);return{focusedIndex:n,handleArrowsKeyDown:a,setFocusedIndex:u}},v=e=>{const{ref:r,keys:n,event:c="keydown"}=e;let u=document;const a=o((e=>{Object.prototype.hasOwnProperty.call(n,e.key)&&n[e.key](e)}),[n]);t((()=>(r&&r.current&&(u=r.current),u.addEventListener(c,a),()=>{u.removeEventListener(c,a)})),[c,a])};var h,w;h=module.exports,w=f,Object.keys(w).forEach((function(e){"default"!==e&&"__esModule"!==e&&Object.defineProperty(h,e,{enumerable:!0,get:function(){return w[e]}})})); | ||
var $jeJr1$react = require("react"); | ||
var $jeJr1$reactdom = require("react-dom"); | ||
var $jeJr1$emotion = require("emotion"); | ||
function $parcel$export(e, n, v, s) { | ||
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); | ||
} | ||
function $parcel$interopDefault(a) { | ||
return a && a.__esModule ? a.default : a; | ||
} | ||
$parcel$export(module.exports, "Portal", () => $b2f7c328a8bc89c1$export$602eac185826482c); | ||
$parcel$export(module.exports, "TabFocusTrap", () => $d29dca464378665a$export$369d2a2b1a53acfd); | ||
$parcel$export(module.exports, "useArrowKeyNavigation", () => $f99b58a4aec25c7e$export$e8d01079a7f85a7); | ||
$parcel$export(module.exports, "useKeyboard", () => $7d270255de91b51a$export$8f71654801c2f7cd); | ||
function $b2f7c328a8bc89c1$export$602eac185826482c({ children: children , container: containerElement }) { | ||
const container = $jeJr1$react.useRef(containerElement); | ||
const portal = $jeJr1$react.useRef(document.createElement('div')); | ||
$jeJr1$react.useEffect(()=>{ | ||
if (!container.current) container.current = document.body; | ||
const portalContainer = portal.current; | ||
portalContainer.setAttribute('data-cf-ui-portal', ''); | ||
container.current.appendChild(portalContainer); | ||
return ()=>{ | ||
if (container.current) container.current.removeChild(portalContainer); | ||
}; | ||
}, []); | ||
return portal.current ? /*#__PURE__*/ $jeJr1$reactdom.createPortal(children, portal.current) : null; | ||
} | ||
function $d29dca464378665a$export$369d2a2b1a53acfd({ className: className , children: children , ...otherProps }) { | ||
return(/*#__PURE__*/ $parcel$interopDefault($jeJr1$react).createElement("span", { | ||
tabIndex: -1, | ||
className: $jeJr1$emotion.cx(/*#__PURE__*/ $jeJr1$emotion.css({ | ||
name: "t8ljbi", | ||
styles: "display:inherit;outline:0;" | ||
}), className), | ||
...otherProps | ||
}, children)); | ||
} | ||
const $f99b58a4aec25c7e$var$ARROW_KEY_TYPES = { | ||
vertical: { | ||
prev: 'ArrowUp', | ||
next: 'ArrowDown' | ||
}, | ||
horizontal: { | ||
prev: 'ArrowLeft', | ||
next: 'ArrowRight' | ||
} | ||
}; | ||
const $f99b58a4aec25c7e$export$e8d01079a7f85a7 = ({ itemsContainerRef: itemsContainerRef , itemsSelector: itemsSelector , keyType: keyType = 'vertical' })=>{ | ||
const [focusedIndex, setFocusedIndex] = $jeJr1$react.useState(0); | ||
const handleArrowsKeyDown = $jeJr1$react.useCallback((event)=>{ | ||
const container = itemsContainerRef.current; | ||
if (!container) return; | ||
const items = container.querySelectorAll(itemsSelector); | ||
if (items.length === 0) return; | ||
const lastItemIndex = items.length - 1; | ||
const focusFirstItem = ()=>setFocusedIndex(0) | ||
; | ||
const focusLastItem = ()=>setFocusedIndex(lastItemIndex) | ||
; | ||
const focusNextItem = ()=>{ | ||
if (focusedIndex === lastItemIndex) focusFirstItem(); | ||
else setFocusedIndex(focusedIndex + 1); | ||
}; | ||
const focusPrevItem = ()=>{ | ||
if (focusedIndex === 0) focusLastItem(); | ||
else setFocusedIndex(focusedIndex - 1); | ||
}; | ||
const keyToFnMap = { | ||
[$f99b58a4aec25c7e$var$ARROW_KEY_TYPES[keyType].next]: focusNextItem, | ||
[$f99b58a4aec25c7e$var$ARROW_KEY_TYPES[keyType].prev]: focusPrevItem | ||
}; | ||
const fn = keyToFnMap[event.key]; | ||
if (fn) { | ||
event.preventDefault(); | ||
fn(); | ||
} | ||
}, [ | ||
focusedIndex, | ||
itemsSelector, | ||
itemsContainerRef, | ||
keyType | ||
]); | ||
return { | ||
focusedIndex: focusedIndex, | ||
handleArrowsKeyDown: handleArrowsKeyDown, | ||
setFocusedIndex: setFocusedIndex | ||
}; | ||
}; | ||
const $7d270255de91b51a$export$8f71654801c2f7cd = (props)=>{ | ||
const { ref: ref , keys: keys , event: event = 'keydown' } = props; | ||
let element = document; | ||
const handleKeyEvent = $jeJr1$react.useCallback((e)=>{ | ||
const isKey = Object.prototype.hasOwnProperty.call(keys, e.key); | ||
if (isKey) keys[e.key](e); | ||
}, [ | ||
keys | ||
]); | ||
$jeJr1$react.useEffect(()=>{ | ||
if (ref && ref.current) element = ref.current; | ||
element.addEventListener(event, handleKeyEvent); | ||
return ()=>{ | ||
element.removeEventListener(event, handleKeyEvent); | ||
}; | ||
}, [ | ||
event, | ||
handleKeyEvent | ||
]); | ||
}; | ||
//# sourceMappingURL=main.js.map |
@@ -1,2 +0,118 @@ | ||
import e,{useRef as r,useEffect as t,useCallback as n,useState as o}from"react";import{createPortal as a}from"react-dom";import s from"@babel/runtime/helpers/esm/extends";import{cx as c,css as u}from"emotion";function i(e,r,t,n){Object.defineProperty(e,r,{get:t,set:n,enumerable:!0,configurable:!0})}var l={};function d({children:e,container:n}){const o=r(n),s=r(document.createElement("div"));return t((()=>{o.current||(o.current=document.body);const e=s.current;return e.setAttribute("data-cf-ui-portal",""),o.current.appendChild(e),()=>{o.current&&o.current.removeChild(e)}}),[]),s.current?a(e,s.current):null}function m({className:r,children:t,...n}){return e.createElement("span",s({tabIndex:-1,className:c(u({name:"t8ljbi",styles:"display:inherit;outline:0;"}),r)},n),t)}i(l,"Portal",(()=>d)),i(l,"TabFocusTrap",(()=>m)),i(l,"useArrowKeyNavigation",(()=>f)),i(l,"useKeyboard",(()=>y));const p={vertical:{prev:"ArrowUp",next:"ArrowDown"},horizontal:{prev:"ArrowLeft",next:"ArrowRight"}},f=({itemsContainerRef:e,itemsSelector:r,keyType:t="vertical"})=>{const[a,s]=o(0),c=n((n=>{const o=e.current;if(!o)return;const c=o.querySelectorAll(r);if(0===c.length)return;const u=c.length-1,i={[p[t].next]:()=>{s(a===u?0:a+1)},[p[t].prev]:()=>{s(0===a?u:a-1)}}[n.key];i&&(n.preventDefault(),i())}),[a,r,e,t]);return{focusedIndex:a,handleArrowsKeyDown:c,setFocusedIndex:s}},y=e=>{const{ref:r,keys:o,event:a="keydown"}=e;let s=document;const c=n((e=>{Object.prototype.hasOwnProperty.call(o,e.key)&&o[e.key](e)}),[o]);t((()=>(r&&r.current&&(s=r.current),s.addEventListener(a,c),()=>{s.removeEventListener(a,c)})),[a,c])};export{d as Portal,m as TabFocusTrap,f as useArrowKeyNavigation,y as useKeyboard}; | ||
import $6KSAN$react, {useRef as $6KSAN$useRef, useEffect as $6KSAN$useEffect, useState as $6KSAN$useState, useCallback as $6KSAN$useCallback} from "react"; | ||
import {createPortal as $6KSAN$createPortal} from "react-dom"; | ||
import {cx as $6KSAN$cx, css as $6KSAN$css} from "emotion"; | ||
function $ff31c9f35937f52f$export$602eac185826482c({ children: children , container: containerElement }) { | ||
const container = $6KSAN$useRef(containerElement); | ||
const portal = $6KSAN$useRef(document.createElement('div')); | ||
$6KSAN$useEffect(()=>{ | ||
if (!container.current) container.current = document.body; | ||
const portalContainer = portal.current; | ||
portalContainer.setAttribute('data-cf-ui-portal', ''); | ||
container.current.appendChild(portalContainer); | ||
return ()=>{ | ||
if (container.current) container.current.removeChild(portalContainer); | ||
}; | ||
}, []); | ||
return portal.current ? /*#__PURE__*/ $6KSAN$createPortal(children, portal.current) : null; | ||
} | ||
function $e598977851a558c1$export$369d2a2b1a53acfd({ className: className , children: children , ...otherProps }) { | ||
return(/*#__PURE__*/ $6KSAN$react.createElement("span", { | ||
tabIndex: -1, | ||
className: $6KSAN$cx(/*#__PURE__*/ $6KSAN$css({ | ||
name: "t8ljbi", | ||
styles: "display:inherit;outline:0;" | ||
}), className), | ||
...otherProps | ||
}, children)); | ||
} | ||
const $296a466826e8d180$var$ARROW_KEY_TYPES = { | ||
vertical: { | ||
prev: 'ArrowUp', | ||
next: 'ArrowDown' | ||
}, | ||
horizontal: { | ||
prev: 'ArrowLeft', | ||
next: 'ArrowRight' | ||
} | ||
}; | ||
const $296a466826e8d180$export$e8d01079a7f85a7 = ({ itemsContainerRef: itemsContainerRef , itemsSelector: itemsSelector , keyType: keyType = 'vertical' })=>{ | ||
const [focusedIndex, setFocusedIndex] = $6KSAN$useState(0); | ||
const handleArrowsKeyDown = $6KSAN$useCallback((event)=>{ | ||
const container = itemsContainerRef.current; | ||
if (!container) return; | ||
const items = container.querySelectorAll(itemsSelector); | ||
if (items.length === 0) return; | ||
const lastItemIndex = items.length - 1; | ||
const focusFirstItem = ()=>setFocusedIndex(0) | ||
; | ||
const focusLastItem = ()=>setFocusedIndex(lastItemIndex) | ||
; | ||
const focusNextItem = ()=>{ | ||
if (focusedIndex === lastItemIndex) focusFirstItem(); | ||
else setFocusedIndex(focusedIndex + 1); | ||
}; | ||
const focusPrevItem = ()=>{ | ||
if (focusedIndex === 0) focusLastItem(); | ||
else setFocusedIndex(focusedIndex - 1); | ||
}; | ||
const keyToFnMap = { | ||
[$296a466826e8d180$var$ARROW_KEY_TYPES[keyType].next]: focusNextItem, | ||
[$296a466826e8d180$var$ARROW_KEY_TYPES[keyType].prev]: focusPrevItem | ||
}; | ||
const fn = keyToFnMap[event.key]; | ||
if (fn) { | ||
event.preventDefault(); | ||
fn(); | ||
} | ||
}, [ | ||
focusedIndex, | ||
itemsSelector, | ||
itemsContainerRef, | ||
keyType | ||
]); | ||
return { | ||
focusedIndex: focusedIndex, | ||
handleArrowsKeyDown: handleArrowsKeyDown, | ||
setFocusedIndex: setFocusedIndex | ||
}; | ||
}; | ||
const $95440ece1fedb657$export$8f71654801c2f7cd = (props)=>{ | ||
const { ref: ref , keys: keys , event: event = 'keydown' } = props; | ||
let element = document; | ||
const handleKeyEvent = $6KSAN$useCallback((e)=>{ | ||
const isKey = Object.prototype.hasOwnProperty.call(keys, e.key); | ||
if (isKey) keys[e.key](e); | ||
}, [ | ||
keys | ||
]); | ||
$6KSAN$useEffect(()=>{ | ||
if (ref && ref.current) element = ref.current; | ||
element.addEventListener(event, handleKeyEvent); | ||
return ()=>{ | ||
element.removeEventListener(event, handleKeyEvent); | ||
}; | ||
}, [ | ||
event, | ||
handleKeyEvent | ||
]); | ||
}; | ||
export {$ff31c9f35937f52f$export$602eac185826482c as Portal, $e598977851a558c1$export$369d2a2b1a53acfd as TabFocusTrap, $296a466826e8d180$export$e8d01079a7f85a7 as useArrowKeyNavigation, $95440ece1fedb657$export$8f71654801c2f7cd as useKeyboard}; | ||
//# sourceMappingURL=module.js.map |
{ | ||
"name": "@contentful/f36-utils", | ||
"version": "4.0.1-next-v4-9789.2434+754dee05", | ||
"version": "4.0.1-next-v4-9898.2438+c14d4c99", | ||
"description": "Forma 36: utils React Component", | ||
@@ -21,3 +21,3 @@ "license": "MIT", | ||
"scripts": { | ||
"build": "parcel build index.ts" | ||
"build": "parcel build" | ||
}, | ||
@@ -35,3 +35,3 @@ "dependencies": { | ||
}, | ||
"gitHead": "754dee05f9e188f1492231120e4449a9e4f1992d" | ||
"gitHead": "c14d4c99c0a87c8905dbe90d5f1dc38dd12d1008" | ||
} |
# @contentful/f36-utils | ||
This package is part of the pre-release. This means it is unsupported and subject to breaking changes without warning. | ||
Please use official, supported version of the library [forma-36](https://github.com/contentful/forma-36/tree/master/packages/forma-36-react-components), [NPM](https://www.npmjs.com/package/@contentful/forma-36-react-components) | ||
## Portal | ||
[Portal] renders a React component tree in a separate part of the DOM. | ||
### How to use Portal | ||
Anything that you put inside the Portal component renders in a `<div data-cf-ui-portal>` element that gets appended to `document.body` by default. You can append to a different container by passing the HTML element of the container as the `container` prop. | ||
### Code examples | ||
```jsx | ||
<Portal>Render in a new tree.</Portal> | ||
``` | ||
Please use official, supported version of the library [forma-36](https://github.com/contentful/forma-36/tree/master/packages/forma-36-react-components), [NPM](https://www.npmjs.com/package/@contentful/forma-36-react-components). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
43130
540
2
6