@wordpress/compose 5.0.1 → 5.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -3
- package/build/higher-order/if-condition/index.js.map +1 -1
- package/build/higher-order/pure/index.js.map +1 -1
- package/build/higher-order/with-instance-id/index.js.map +1 -1
- package/build/higher-order/with-safe-timeout/index.js.map +1 -1
- package/build/hooks/use-constrained-tabbing/index.js +46 -43
- package/build/hooks/use-constrained-tabbing/index.js.map +1 -1
- package/build/hooks/use-debounce/index.js +1 -1
- package/build/hooks/use-debounce/index.js.map +1 -1
- package/build/hooks/use-drop-zone/index.js +12 -6
- package/build/hooks/use-drop-zone/index.js.map +1 -1
- package/build/hooks/use-fixed-window-list/index.js +175 -0
- package/build/hooks/use-fixed-window-list/index.js.map +1 -0
- package/build/hooks/use-focusable-iframe/index.js +50 -0
- package/build/hooks/use-focusable-iframe/index.js.map +1 -0
- package/build/hooks/use-previous/index.js +3 -14
- package/build/hooks/use-previous/index.js.map +1 -1
- package/build/hooks/use-reduced-motion/index.js +1 -7
- package/build/hooks/use-reduced-motion/index.js.map +1 -1
- package/build/hooks/use-throttle/index.js +1 -1
- package/build/hooks/use-throttle/index.js.map +1 -1
- package/build/index.js +16 -0
- package/build/index.js.map +1 -1
- package/build-module/higher-order/if-condition/index.js +1 -1
- package/build-module/higher-order/if-condition/index.js.map +1 -1
- package/build-module/higher-order/pure/index.js +1 -1
- package/build-module/higher-order/pure/index.js.map +1 -1
- package/build-module/higher-order/with-instance-id/index.js +1 -2
- package/build-module/higher-order/with-instance-id/index.js.map +1 -1
- package/build-module/higher-order/with-safe-timeout/index.js +1 -1
- package/build-module/higher-order/with-safe-timeout/index.js.map +1 -1
- package/build-module/hooks/use-constrained-tabbing/index.js +41 -40
- package/build-module/hooks/use-constrained-tabbing/index.js.map +1 -1
- package/build-module/hooks/use-debounce/index.js +1 -1
- package/build-module/hooks/use-debounce/index.js.map +1 -1
- package/build-module/hooks/use-drop-zone/index.js +12 -6
- package/build-module/hooks/use-drop-zone/index.js.map +1 -1
- package/build-module/hooks/use-fixed-window-list/index.js +164 -0
- package/build-module/hooks/use-fixed-window-list/index.js.map +1 -0
- package/build-module/hooks/use-focusable-iframe/index.js +40 -0
- package/build-module/hooks/use-focusable-iframe/index.js.map +1 -0
- package/build-module/hooks/use-previous/index.js +3 -14
- package/build-module/hooks/use-previous/index.js.map +1 -1
- package/build-module/hooks/use-reduced-motion/index.js +2 -8
- package/build-module/hooks/use-reduced-motion/index.js.map +1 -1
- package/build-module/hooks/use-throttle/index.js +1 -1
- package/build-module/hooks/use-throttle/index.js.map +1 -1
- package/build-module/index.js +2 -0
- package/build-module/index.js.map +1 -1
- package/build-types/higher-order/if-condition/index.d.ts.map +1 -1
- package/build-types/higher-order/pure/index.d.ts.map +1 -1
- package/build-types/higher-order/with-instance-id/index.d.ts.map +1 -1
- package/build-types/higher-order/with-safe-timeout/index.d.ts.map +1 -1
- package/build-types/hooks/use-constrained-tabbing/index.d.ts.map +1 -1
- package/build-types/hooks/use-debounce/index.d.ts +2 -2
- package/build-types/hooks/use-debounce/index.d.ts.map +1 -1
- package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
- package/build-types/hooks/use-fixed-window-list/index.d.ts +57 -0
- package/build-types/hooks/use-fixed-window-list/index.d.ts.map +1 -0
- package/build-types/hooks/use-focus-outside/index.d.ts +3 -3
- package/build-types/hooks/use-focus-outside/index.d.ts.map +1 -1
- package/build-types/hooks/use-focusable-iframe/index.d.ts +8 -0
- package/build-types/hooks/use-focusable-iframe/index.d.ts.map +1 -0
- package/build-types/hooks/use-previous/index.d.ts +2 -4
- package/build-types/hooks/use-previous/index.d.ts.map +1 -1
- package/build-types/hooks/use-reduced-motion/index.d.ts +5 -0
- package/build-types/hooks/use-reduced-motion/index.d.ts.map +1 -1
- package/build-types/hooks/use-throttle/index.d.ts +2 -2
- package/build-types/hooks/use-throttle/index.d.ts.map +1 -1
- package/build-types/hooks/use-viewport-match/index.d.ts +2 -2
- package/build-types/hooks/use-viewport-match/index.d.ts.map +1 -1
- package/build-types/index.d.ts +2 -0
- package/package.json +9 -9
- package/src/higher-order/if-condition/index.tsx +0 -1
- package/src/higher-order/pure/index.tsx +0 -1
- package/src/higher-order/with-instance-id/index.tsx +0 -1
- package/src/higher-order/with-safe-timeout/index.tsx +0 -1
- package/src/hooks/use-constrained-tabbing/index.js +39 -33
- package/src/hooks/use-debounce/index.js +1 -1
- package/src/hooks/use-drop-zone/index.js +11 -9
- package/src/hooks/use-fixed-window-list/index.js +174 -0
- package/src/hooks/use-focusable-iframe/README.md +29 -0
- package/src/hooks/use-focusable-iframe/index.js +34 -0
- package/src/hooks/use-previous/index.ts +24 -0
- package/src/hooks/use-reduced-motion/index.js +2 -13
- package/src/hooks/use-throttle/index.js +1 -1
- package/src/index.js +2 -0
- package/tsconfig.tsbuildinfo +1 -1694
- package/src/hooks/use-previous/index.js +0 -31
package/README.md
CHANGED
|
@@ -53,7 +53,7 @@ Install the module
|
|
|
53
53
|
npm install @wordpress/compose --save
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for
|
|
56
|
+
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
|
|
57
57
|
|
|
58
58
|
## API
|
|
59
59
|
|
|
@@ -207,7 +207,16 @@ _Parameters_
|
|
|
207
207
|
|
|
208
208
|
_Returns_
|
|
209
209
|
|
|
210
|
-
- `
|
|
210
|
+
- `import('lodash').DebouncedFunc<TFunc>`: Debounced function.
|
|
211
|
+
|
|
212
|
+
### useFocusableIframe
|
|
213
|
+
|
|
214
|
+
Dispatches a bubbling focus event when the iframe receives focus. Use
|
|
215
|
+
`onFocus` as usual on the iframe or a parent element.
|
|
216
|
+
|
|
217
|
+
_Returns_
|
|
218
|
+
|
|
219
|
+
- `Object`: Ref to pass to the iframe.
|
|
211
220
|
|
|
212
221
|
### useFocusOnMount
|
|
213
222
|
|
|
@@ -452,7 +461,7 @@ _Parameters_
|
|
|
452
461
|
|
|
453
462
|
_Returns_
|
|
454
463
|
|
|
455
|
-
- `
|
|
464
|
+
- `import('lodash').DebouncedFunc<TFunc>`: Throttled function.
|
|
456
465
|
|
|
457
466
|
### useViewportMatch
|
|
458
467
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/higher-order/if-condition/index.tsx"],"names":["ifCondition","predicate","WrappedComponent","props"],"mappings":";;;;;;;;;;;AAGA;;AAHA;AACA;AACA;;
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/higher-order/if-condition/index.tsx"],"names":["ifCondition","predicate","WrappedComponent","props"],"mappings":";;;;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,WAAW,GAChBC,SADmB,IAGnB,yCACGC,gBAAF,IAA0BC,KAAF,IAAqB;AAC5C,MAAK,CAAEF,SAAS,CAAEE,KAAF,CAAhB,EAA4B;AAC3B,WAAO,IAAP;AACA;;AAED,SAAO,4BAAC,gBAAD,EAAuBA,KAAvB,CAAP;AACA,CAPF,EAQC,aARD,CAHD;;eAceH,W","sourcesContent":["/**\n * Internal dependencies\n */\nimport createHigherOrderComponent from '../../utils/create-higher-order-component';\nimport type { HigherOrderComponent } from '../../utils/create-higher-order-component';\n\n/**\n * Higher-order component creator, creating a new component which renders if\n * the given condition is satisfied or with the given optional prop name.\n *\n * @example\n * ```ts\n * type Props = { foo: string };\n * const Component = ( props: Props ) => <div>{ props.foo }</div>;\n * const ConditionalComponent = ifCondition( ( props: Props ) => props.foo.length !== 0 )( Component );\n * <ConditionalComponent foo=\"\" />; // => null\n * <ConditionalComponent foo=\"bar\" />; // => <div>bar</div>;\n * ```\n *\n * @param predicate Function to test condition.\n *\n * @return Higher-order component.\n */\nconst ifCondition = < TProps, >(\n\tpredicate: ( props: TProps ) => boolean\n): HigherOrderComponent< TProps, TProps > =>\n\tcreateHigherOrderComponent(\n\t\t( WrappedComponent ) => ( props: TProps ) => {\n\t\t\tif ( ! predicate( props ) ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\treturn <WrappedComponent { ...props } />;\n\t\t},\n\t\t'ifCondition'\n\t);\n\nexport default ifCondition;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/higher-order/pure/index.tsx"],"names":["pure","Wrapped","prototype","Component","shouldComponentUpdate","nextProps","nextState","props","state","render"],"mappings":";;;;;;;;;AAIA;;AADA;;AAMA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/higher-order/pure/index.tsx"],"names":["pure","Wrapped","prototype","Component","shouldComponentUpdate","nextProps","nextState","props","state","render"],"mappings":";;;;;;;;;AAIA;;AADA;;AAMA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAUA;AACA;AACA;AACA;AACA,MAAMA,IAAgC,GAAG,yCAC3BC,OAAb,IAAmD;AAClD,MAAKA,OAAO,CAACC,SAAR,YAA6BC,kBAAlC,EAA8C;AAC7C,WAAO,cAAgBF,OAAhB,CAAsD;AAC5DG,MAAAA,qBAAqB,CAAEC,SAAF,EAAqBC,SAArB,EAAsC;AAC1D,eACC,CAAE,6BAAgBD,SAAhB,EAA2B,KAAKE,KAAhC,CAAF,IACA,CAAE,6BAAgBD,SAAhB,EAA2B,KAAKE,KAAhC,CAFH;AAIA;;AAN2D,KAA7D;AAQA;;AAED,SAAO,cAAcL,kBAAd,CAAkC;AACxCC,IAAAA,qBAAqB,CAAEC,SAAF,EAAsB;AAC1C,aAAO,CAAE,6BAAgBA,SAAhB,EAA2B,KAAKE,KAAhC,CAAT;AACA;;AAEDE,IAAAA,MAAM,GAAG;AACR,aAAO,4BAAC,OAAD,EAAc,KAAKF,KAAnB,CAAP;AACA;;AAPuC,GAAzC;AASA,CAtBuC,EAuBxC,MAvBwC,CAAzC;eA0BeP,I","sourcesContent":["/**\n * WordPress dependencies\n */\nimport isShallowEqual from '@wordpress/is-shallow-equal';\nimport { Component } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport createHigherOrderComponent from '../../utils/create-higher-order-component';\nimport type { SimpleHigherOrderComponent } from '../../utils/create-higher-order-component';\n\n/**\n * External dependencies\n */\n// eslint-disable-next-line no-restricted-imports\nimport type { ComponentType, ComponentClass } from 'react';\n\n/**\n * Given a component returns the enhanced component augmented with a component\n * only rerendering when its props/state change\n */\nconst pure: SimpleHigherOrderComponent = createHigherOrderComponent(\n\t< TProps, >( Wrapped: ComponentType< TProps > ) => {\n\t\tif ( Wrapped.prototype instanceof Component ) {\n\t\t\treturn class extends ( Wrapped as ComponentClass< TProps > ) {\n\t\t\t\tshouldComponentUpdate( nextProps: TProps, nextState: any ) {\n\t\t\t\t\treturn (\n\t\t\t\t\t\t! isShallowEqual( nextProps, this.props ) ||\n\t\t\t\t\t\t! isShallowEqual( nextState, this.state )\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\n\t\treturn class extends Component< TProps > {\n\t\t\tshouldComponentUpdate( nextProps: TProps ) {\n\t\t\t\treturn ! isShallowEqual( nextProps, this.props );\n\t\t\t}\n\n\t\t\trender() {\n\t\t\t\treturn <Wrapped { ...this.props } />;\n\t\t\t}\n\t\t};\n\t},\n\t'pure'\n);\n\nexport default pure;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/higher-order/with-instance-id/index.tsx"],"names":["withInstanceId","WrappedComponent","props","instanceId"],"mappings":";;;;;;;;;;;;;AASA;;
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/higher-order/with-instance-id/index.tsx"],"names":["withInstanceId","WrappedComponent","props","instanceId"],"mappings":";;;;;;;;;;;;;AASA;;AAEA;;AAXA;AACA;AACA;AACA;;AAGA;AACA;AACA;;AAKA;AACA;AACA;AACA;AACA,MAAMA,cAEH,GAAG,yCAEJC,gBADD,IAEK;AACJ,SAASC,KAAF,IAA2C;AACjD,UAAMC,UAAU,GAAG,4BAAeF,gBAAf,CAAnB;AACA,WACC,4BAAC,gBAAD,6BACQC,KADR;AAEC,MAAA,UAAU,EAAGC;AAFd,OADD;AAMA,GARD;AASA,CAbI,EAcL,gBAdK,CAFN;eAmBeH,c","sourcesContent":["/**\n * External dependencies\n */\n// eslint-disable-next-line no-restricted-imports\nimport type { ComponentType } from 'react';\n\n/**\n * Internal dependencies\n */\nimport createHigherOrderComponent from '../../utils/create-higher-order-component';\nimport type { PropInjectingHigherOrderComponent } from '../../utils/create-higher-order-component';\nimport useInstanceId from '../../hooks/use-instance-id';\n\n/**\n * A Higher Order Component used to be provide a unique instance ID by\n * component.\n */\nconst withInstanceId: PropInjectingHigherOrderComponent< {\n\tinstanceId: string | number;\n} > = createHigherOrderComponent(\n\t< TProps extends { instanceId: string | number } >(\n\t\tWrappedComponent: ComponentType< TProps >\n\t) => {\n\t\treturn ( props: Omit< TProps, 'instanceId' > ) => {\n\t\t\tconst instanceId = useInstanceId( WrappedComponent );\n\t\t\treturn (\n\t\t\t\t<WrappedComponent\n\t\t\t\t\t{ ...( props as TProps ) }\n\t\t\t\t\tinstanceId={ instanceId }\n\t\t\t\t/>\n\t\t\t);\n\t\t};\n\t},\n\t'withInstanceId'\n);\n\nexport default withInstanceId;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/higher-order/with-safe-timeout/index.tsx"],"names":["withSafeTimeout","OriginalComponent","WrappedComponent","Component","constructor","props","timeouts","setTimeout","bind","clearTimeout","componentWillUnmount","forEach","fn","delay","id","push","render"],"mappings":";;;;;;;;;AAUA;;AAPA;;AAYA;;AAfA;AACA;AACA;;AAKA;AACA;AACA;;AAGA;AACA;AACA;;
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/higher-order/with-safe-timeout/index.tsx"],"names":["withSafeTimeout","OriginalComponent","WrappedComponent","Component","constructor","props","timeouts","setTimeout","bind","clearTimeout","componentWillUnmount","forEach","fn","delay","id","push","render"],"mappings":";;;;;;;;;AAUA;;AAPA;;AAYA;;AAfA;AACA;AACA;;AAKA;AACA;AACA;;AAGA;AACA;AACA;;AAiBA;AACA;AACA;AACA;AACA,MAAMA,eAAkE,GAAG,yCAEzEC,iBADD,IAEK;AACJ,SAAO,MAAMC,gBAAN,SAA+BC,kBAA/B,CAEL;AAGDC,IAAAA,WAAW,CAAEC,KAAF,EAAe;AACzB,YAAOA,KAAP;AACA,WAAKC,QAAL,GAAgB,EAAhB;AACA,WAAKC,UAAL,GAAkB,KAAKA,UAAL,CAAgBC,IAAhB,CAAsB,IAAtB,CAAlB;AACA,WAAKC,YAAL,GAAoB,KAAKA,YAAL,CAAkBD,IAAlB,CAAwB,IAAxB,CAApB;AACA;;AAEDE,IAAAA,oBAAoB,GAAG;AACtB,WAAKJ,QAAL,CAAcK,OAAd,CAAuBF,YAAvB;AACA;;AAEDF,IAAAA,UAAU,CAAEK,EAAF,EAAkCC,KAAlC,EAAkD;AAC3D,YAAMC,EAAE,GAAGP,UAAU,CAAE,MAAM;AAC5BK,QAAAA,EAAE;AACF,aAAKH,YAAL,CAAmBK,EAAnB;AACA,OAHoB,EAGlBD,KAHkB,CAArB;AAIA,WAAKP,QAAL,CAAcS,IAAd,CAAoBD,EAApB;AACA,aAAOA,EAAP;AACA;;AAEDL,IAAAA,YAAY,CAAEK,EAAF,EAAe;AAC1BL,MAAAA,YAAY,CAAEK,EAAF,CAAZ;AACA,WAAKR,QAAL,GAAgB,qBAAS,KAAKA,QAAd,EAAwBQ,EAAxB,CAAhB;AACA;;AAEDE,IAAAA,MAAM,GAAG;AACR,YAAMX,KAAK,GAAG,EACb,GAAG,KAAKA,KADK;AAEbE,QAAAA,UAAU,EAAE,KAAKA,UAFJ;AAGbE,QAAAA,YAAY,EAAE,KAAKA;AAHN,OAAd;AAKA,aAAO,4BAAC,iBAAD,EAA0BJ,KAA1B,CAAP;AACA;;AAnCA,GAFF;AAuCA,CA3CyE,EA4C1E,iBA5C0E,CAA3E;eA+CeL,e","sourcesContent":["/**\n * External dependencies\n */\nimport { without } from 'lodash';\n// eslint-disable-next-line no-restricted-imports\nimport type { ComponentType } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { Component } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport createHigherOrderComponent from '../../utils/create-higher-order-component';\nimport type { PropInjectingHigherOrderComponent } from '../../utils/create-higher-order-component';\n\n/**\n * We cannot use the `Window['setTimeout']` and `Window['clearTimeout']`\n * types here because those functions include functionality that is not handled\n * by this component, like the ability to pass extra arguments.\n *\n * In the case of this component, we only handle the simplest case where\n * `setTimeout` only accepts a function (not a string) and an optional delay.\n */\ntype TimeoutProps = {\n\tsetTimeout: ( fn: () => void, delay: number ) => number;\n\tclearTimeout: ( id: number ) => void;\n};\n\n/**\n * A higher-order component used to provide and manage delayed function calls\n * that ought to be bound to a component's lifecycle.\n */\nconst withSafeTimeout: PropInjectingHigherOrderComponent< TimeoutProps > = createHigherOrderComponent(\n\t< TProps extends TimeoutProps >(\n\t\tOriginalComponent: ComponentType< TProps >\n\t) => {\n\t\treturn class WrappedComponent extends Component<\n\t\t\tOmit< TProps, keyof TimeoutProps >\n\t\t> {\n\t\t\ttimeouts: number[];\n\n\t\t\tconstructor( props: any ) {\n\t\t\t\tsuper( props );\n\t\t\t\tthis.timeouts = [];\n\t\t\t\tthis.setTimeout = this.setTimeout.bind( this );\n\t\t\t\tthis.clearTimeout = this.clearTimeout.bind( this );\n\t\t\t}\n\n\t\t\tcomponentWillUnmount() {\n\t\t\t\tthis.timeouts.forEach( clearTimeout );\n\t\t\t}\n\n\t\t\tsetTimeout( fn: ( ...args: any[] ) => void, delay: number ) {\n\t\t\t\tconst id = setTimeout( () => {\n\t\t\t\t\tfn();\n\t\t\t\t\tthis.clearTimeout( id );\n\t\t\t\t}, delay );\n\t\t\t\tthis.timeouts.push( id );\n\t\t\t\treturn id;\n\t\t\t}\n\n\t\t\tclearTimeout( id: number ) {\n\t\t\t\tclearTimeout( id );\n\t\t\t\tthis.timeouts = without( this.timeouts, id );\n\t\t\t}\n\n\t\t\trender() {\n\t\t\t\tconst props = {\n\t\t\t\t\t...this.props,\n\t\t\t\t\tsetTimeout: this.setTimeout,\n\t\t\t\t\tclearTimeout: this.clearTimeout,\n\t\t\t\t};\n\t\t\t\treturn <OriginalComponent { ...( props as TProps ) } />;\n\t\t\t}\n\t\t};\n\t},\n\t'withSafeTimeout'\n);\n\nexport default withSafeTimeout;\n"]}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
3
5
|
Object.defineProperty(exports, "__esModule", {
|
|
4
6
|
value: true
|
|
5
7
|
});
|
|
@@ -9,12 +11,16 @@ var _keycodes = require("@wordpress/keycodes");
|
|
|
9
11
|
|
|
10
12
|
var _dom = require("@wordpress/dom");
|
|
11
13
|
|
|
12
|
-
var
|
|
14
|
+
var _useRefEffect = _interopRequireDefault(require("../use-ref-effect"));
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
17
|
* WordPress dependencies
|
|
16
18
|
*/
|
|
17
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Internal dependencies
|
|
22
|
+
*/
|
|
23
|
+
|
|
18
24
|
/**
|
|
19
25
|
* In Dialogs/modals, the tabbing must be constrained to the content of
|
|
20
26
|
* the wrapper element. This hook adds the behavior to the returned ref.
|
|
@@ -37,58 +43,55 @@ var _element = require("@wordpress/element");
|
|
|
37
43
|
* ```
|
|
38
44
|
*/
|
|
39
45
|
function useConstrainedTabbing() {
|
|
40
|
-
|
|
41
|
-
/** @type {
|
|
46
|
+
return (0, _useRefEffect.default)(
|
|
47
|
+
/** @type {HTMLElement} */
|
|
42
48
|
node => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
/** @type {number|undefined} */
|
|
50
|
+
let timeoutId;
|
|
51
|
+
|
|
52
|
+
function onKeyDown(
|
|
53
|
+
/** @type {KeyboardEvent} */
|
|
54
|
+
event) {
|
|
55
|
+
const {
|
|
56
|
+
keyCode,
|
|
57
|
+
shiftKey,
|
|
58
|
+
target
|
|
59
|
+
} = event;
|
|
60
|
+
|
|
61
|
+
if (keyCode !== _keycodes.TAB) {
|
|
51
62
|
return;
|
|
52
63
|
}
|
|
53
64
|
|
|
54
|
-
|
|
65
|
+
const action = shiftKey ? 'findPrevious' : 'findNext';
|
|
66
|
+
const nextElement = _dom.focus.tabbable[action](
|
|
67
|
+
/** @type {HTMLElement} */
|
|
68
|
+
target) || null; // If the element that is about to receive focus is outside the
|
|
69
|
+
// area, move focus to a div and insert it at the start or end of
|
|
70
|
+
// the area, depending on the direction. Without preventing default
|
|
71
|
+
// behaviour, the browser will then move focus to the next element.
|
|
72
|
+
|
|
73
|
+
if (node.contains(nextElement)) {
|
|
55
74
|
return;
|
|
56
75
|
}
|
|
57
76
|
|
|
58
|
-
const
|
|
77
|
+
const domAction = shiftKey ? 'append' : 'prepend';
|
|
78
|
+
const {
|
|
79
|
+
ownerDocument
|
|
80
|
+
} = node;
|
|
81
|
+
const trap = ownerDocument.createElement('div');
|
|
82
|
+
trap.tabIndex = -1;
|
|
83
|
+
node[domAction](trap);
|
|
84
|
+
trap.focus(); // Remove after the browser moves focus to the next element.
|
|
59
85
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
86
|
+
timeoutId = setTimeout(() => node.removeChild(trap));
|
|
87
|
+
}
|
|
63
88
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
/** @type {HTMLElement} */
|
|
70
|
-
|
|
71
|
-
lastTabbable.focus();
|
|
72
|
-
} else if (!event.shiftKey && event.target === lastTabbable) {
|
|
73
|
-
event.preventDefault();
|
|
74
|
-
/** @type {HTMLElement} */
|
|
75
|
-
|
|
76
|
-
firstTabbable.focus();
|
|
77
|
-
/*
|
|
78
|
-
* When pressing Tab and none of the tabbables has focus, the keydown
|
|
79
|
-
* event happens on the wrapper div: move focus on the first tabbable.
|
|
80
|
-
*/
|
|
81
|
-
} else if (!tabbables.includes(
|
|
82
|
-
/** @type {Element} */
|
|
83
|
-
event.target)) {
|
|
84
|
-
event.preventDefault();
|
|
85
|
-
/** @type {HTMLElement} */
|
|
86
|
-
|
|
87
|
-
firstTabbable.focus();
|
|
88
|
-
}
|
|
89
|
-
});
|
|
89
|
+
node.addEventListener('keydown', onKeyDown);
|
|
90
|
+
return () => {
|
|
91
|
+
node.removeEventListener('keydown', onKeyDown);
|
|
92
|
+
clearTimeout(timeoutId);
|
|
93
|
+
};
|
|
90
94
|
}, []);
|
|
91
|
-
return ref;
|
|
92
95
|
}
|
|
93
96
|
|
|
94
97
|
var _default = useConstrainedTabbing;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-constrained-tabbing/index.js"],"names":["useConstrainedTabbing","
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-constrained-tabbing/index.js"],"names":["useConstrainedTabbing","node","timeoutId","onKeyDown","event","keyCode","shiftKey","target","TAB","action","nextElement","focus","tabbable","contains","domAction","ownerDocument","trap","createElement","tabIndex","setTimeout","removeChild","addEventListener","removeEventListener","clearTimeout"],"mappings":";;;;;;;;;AAGA;;AACA;;AAKA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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,EAyCJ,EAzCI,CAAP;AA0CA;;eAEcF,qB","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { TAB } from '@wordpress/keycodes';\nimport { focus } from '@wordpress/dom';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\n\n/**\n * In Dialogs/modals, the tabbing must be constrained to the content of\n * the wrapper element. This hook adds the behavior to the returned ref.\n *\n * @return {import('react').RefCallback<Element>} Element Ref.\n *\n * @example\n * ```js\n * import { useConstrainedTabbing } from '@wordpress/compose';\n *\n * const ConstrainedTabbingExample = () => {\n * const constrainedTabbingRef = useConstrainedTabbing()\n * return (\n * <div ref={ constrainedTabbingRef }>\n * <Button />\n * <Button />\n * </div>\n * );\n * }\n * ```\n */\nfunction useConstrainedTabbing() {\n\treturn useRefEffect( ( /** @type {HTMLElement} */ node ) => {\n\t\t/** @type {number|undefined} */\n\t\tlet timeoutId;\n\n\t\tfunction onKeyDown( /** @type {KeyboardEvent} */ event ) {\n\t\t\tconst { keyCode, shiftKey, target } = event;\n\n\t\t\tif ( keyCode !== TAB ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst action = shiftKey ? 'findPrevious' : 'findNext';\n\t\t\tconst nextElement =\n\t\t\t\tfocus.tabbable[ action ](\n\t\t\t\t\t/** @type {HTMLElement} */ ( target )\n\t\t\t\t) || null;\n\n\t\t\t// If the element that is about to receive focus is outside the\n\t\t\t// area, move focus to a div and insert it at the start or end of\n\t\t\t// the area, depending on the direction. Without preventing default\n\t\t\t// behaviour, the browser will then move focus to the next element.\n\t\t\tif ( node.contains( nextElement ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst domAction = shiftKey ? 'append' : 'prepend';\n\t\t\tconst { ownerDocument } = node;\n\t\t\tconst trap = ownerDocument.createElement( 'div' );\n\n\t\t\ttrap.tabIndex = -1;\n\t\t\tnode[ domAction ]( trap );\n\t\t\ttrap.focus();\n\t\t\t// Remove after the browser moves focus to the next element.\n\t\t\ttimeoutId = setTimeout( () => node.removeChild( trap ) );\n\t\t}\n\n\t\tnode.addEventListener( 'keydown', onKeyDown );\n\t\treturn () => {\n\t\t\tnode.removeEventListener( 'keydown', onKeyDown );\n\t\t\tclearTimeout( timeoutId );\n\t\t};\n\t}, [] );\n}\n\nexport default useConstrainedTabbing;\n"]}
|
|
@@ -34,7 +34,7 @@ var _element = require("@wordpress/element");
|
|
|
34
34
|
* @param {TFunc} fn The function to debounce.
|
|
35
35
|
* @param {number} [wait] The number of milliseconds to delay.
|
|
36
36
|
* @param {import('lodash').DebounceSettings} [options] The options object.
|
|
37
|
-
* @return {
|
|
37
|
+
* @return {import('lodash').DebouncedFunc<TFunc>} Debounced function.
|
|
38
38
|
*/
|
|
39
39
|
function useDebounce(fn, wait, options) {
|
|
40
40
|
/* eslint-enable jsdoc/valid-types */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-debounce/index.js"],"names":["useDebounce","fn","wait","options","debounced","cancel"],"mappings":";;;;;;;AAGA;;AACA;;AAKA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,WAAT,CAAsBC,EAAtB,EAA0BC,IAA1B,EAAgCC,OAAhC,EAA0C;AACxD;AACA,QAAMC,SAAS,GAAG,4BAAY,MAAM,sBAAUH,EAAV,EAAcC,IAAd,EAAoBC,OAApB,CAAlB,EAAiD,CAClEF,EADkE,EAElEC,IAFkE,EAGlEC,OAHkE,CAAjD,CAAlB;AAKA,0BAAW,MAAM,MAAMC,SAAS,CAACC,MAAV,EAAvB,EAA2C,CAAED,SAAF,CAA3C;AACA,SAAOA,SAAP;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { debounce } from 'lodash';\nimport { useMemoOne } from 'use-memo-one';\n\n/**\n * WordPress dependencies\n */\nimport { useEffect } from '@wordpress/element';\n\n/* eslint-disable jsdoc/valid-types */\n/**\n * Debounces a function with Lodash's `debounce`. A new debounced function will\n * be returned and any scheduled calls cancelled if any of the arguments change,\n * including the function to debounce, so please wrap functions created on\n * render in components in `useCallback`.\n *\n * @see https://docs-lodash.com/v4/debounce/\n *\n * @template {(...args: any[]) => void} TFunc\n *\n * @param {TFunc} fn The function to debounce.\n * @param {number} [wait] The number of milliseconds to delay.\n * @param {import('lodash').DebounceSettings} [options] The options object.\n * @return {
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-debounce/index.js"],"names":["useDebounce","fn","wait","options","debounced","cancel"],"mappings":";;;;;;;AAGA;;AACA;;AAKA;;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,WAAT,CAAsBC,EAAtB,EAA0BC,IAA1B,EAAgCC,OAAhC,EAA0C;AACxD;AACA,QAAMC,SAAS,GAAG,4BAAY,MAAM,sBAAUH,EAAV,EAAcC,IAAd,EAAoBC,OAApB,CAAlB,EAAiD,CAClEF,EADkE,EAElEC,IAFkE,EAGlEC,OAHkE,CAAjD,CAAlB;AAKA,0BAAW,MAAM,MAAMC,SAAS,CAACC,MAAV,EAAvB,EAA2C,CAAED,SAAF,CAA3C;AACA,SAAOA,SAAP;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { debounce } from 'lodash';\nimport { useMemoOne } from 'use-memo-one';\n\n/**\n * WordPress dependencies\n */\nimport { useEffect } from '@wordpress/element';\n\n/* eslint-disable jsdoc/valid-types */\n/**\n * Debounces a function with Lodash's `debounce`. A new debounced function will\n * be returned and any scheduled calls cancelled if any of the arguments change,\n * including the function to debounce, so please wrap functions created on\n * render in components in `useCallback`.\n *\n * @see https://docs-lodash.com/v4/debounce/\n *\n * @template {(...args: any[]) => void} TFunc\n *\n * @param {TFunc} fn The function to debounce.\n * @param {number} [wait] The number of milliseconds to delay.\n * @param {import('lodash').DebounceSettings} [options] The options object.\n * @return {import('lodash').DebouncedFunc<TFunc>} Debounced function.\n */\nexport default function useDebounce( fn, wait, options ) {\n\t/* eslint-enable jsdoc/valid-types */\n\tconst debounced = useMemoOne( () => debounce( fn, wait, options ), [\n\t\tfn,\n\t\twait,\n\t\toptions,\n\t] );\n\tuseEffect( () => () => debounced.cancel(), [ debounced ] );\n\treturn debounced;\n}\n"]}
|
|
@@ -87,15 +87,23 @@ function useDropZone({
|
|
|
87
87
|
/**
|
|
88
88
|
* Checks if an element is in the drop zone.
|
|
89
89
|
*
|
|
90
|
-
* @param {
|
|
90
|
+
* @param {EventTarget|null} targetToCheck
|
|
91
91
|
*
|
|
92
92
|
* @return {boolean} True if in drop zone, false if not.
|
|
93
93
|
*/
|
|
94
94
|
|
|
95
|
-
function isElementInZone(
|
|
96
|
-
|
|
95
|
+
function isElementInZone(targetToCheck) {
|
|
96
|
+
const {
|
|
97
|
+
defaultView
|
|
98
|
+
} = ownerDocument;
|
|
99
|
+
|
|
100
|
+
if (!targetToCheck || !defaultView || !(targetToCheck instanceof defaultView.HTMLElement) || !element.contains(targetToCheck)) {
|
|
97
101
|
return false;
|
|
98
102
|
}
|
|
103
|
+
/** @type {HTMLElement|null} */
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
let elementToCheck = targetToCheck;
|
|
99
107
|
|
|
100
108
|
do {
|
|
101
109
|
if (elementToCheck.dataset.isDropZone) {
|
|
@@ -167,9 +175,7 @@ function useDropZone({
|
|
|
167
175
|
// leaving the drop zone, which means the `relatedTarget`
|
|
168
176
|
// (element that has been entered) should be outside the drop
|
|
169
177
|
// zone.
|
|
170
|
-
if (isElementInZone(
|
|
171
|
-
/** @type {HTMLElement|null} */
|
|
172
|
-
event.relatedTarget)) {
|
|
178
|
+
if (isElementInZone(event.relatedTarget)) {
|
|
173
179
|
return;
|
|
174
180
|
}
|
|
175
181
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-drop-zone/index.js"],"names":["useFreshRef","value","ref","current","useDropZone","isDisabled","onDrop","_onDrop","onDragStart","_onDragStart","onDragEnter","_onDragEnter","onDragLeave","_onDragLeave","onDragEnd","_onDragEnd","onDragOver","_onDragOver","onDropRef","onDragStartRef","onDragEnterRef","onDragLeaveRef","onDragEndRef","onDragOverRef","element","isDragging","ownerDocument","isElementInZone","elementToCheck","contains","dataset","isDropZone","parentElement","maybeDragStart","event","removeEventListener","addEventListener","maybeDragEnd","preventDefault","relatedTarget","defaultPrevented","dataTransfer","files","length"],"mappings":";;;;;;;;;AAGA;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,WAAT,CAAsBC,KAAtB,EAA8B;AAC7B;;AACA;;AACA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,QAAMC,GAAG,GAAG,sBAAZ;AACAA,EAAAA,GAAG,CAACC,OAAJ,GAAcF,KAAd;AACA,SAAOC,GAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACe,SAASE,WAAT,CAAsB;AACpCC,EAAAA,UADoC;AAEpCC,EAAAA,MAAM,EAAEC,OAF4B;AAGpCC,EAAAA,WAAW,EAAEC,YAHuB;AAIpCC,EAAAA,WAAW,EAAEC,YAJuB;AAKpCC,EAAAA,WAAW,EAAEC,YALuB;AAMpCC,EAAAA,SAAS,EAAEC,UANyB;AAOpCC,EAAAA,UAAU,EAAEC;AAPwB,CAAtB,EAQX;AACH,QAAMC,SAAS,GAAGlB,WAAW,CAAEO,OAAF,CAA7B;AACA,QAAMY,cAAc,GAAGnB,WAAW,CAAES,YAAF,CAAlC;AACA,QAAMW,cAAc,GAAGpB,WAAW,CAAEW,YAAF,CAAlC;AACA,QAAMU,cAAc,GAAGrB,WAAW,CAAEa,YAAF,CAAlC;AACA,QAAMS,YAAY,GAAGtB,WAAW,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cesContent":["/**\n * WordPress dependencies\n */\nimport { useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\n\n/* eslint-disable jsdoc/valid-types */\n/**\n * @template T\n * @param {T} value\n * @return {import('react').MutableRefObject<T>} A ref with the value.\n */\nfunction useFreshRef( value ) {\n\t/* eslint-enable jsdoc/valid-types */\n\t/* eslint-disable jsdoc/no-undefined-types */\n\t/** @type {import('react').MutableRefObject<T>} */\n\t/* eslint-enable jsdoc/no-undefined-types */\n\t// Disable reason: We're doing something pretty JavaScript-y here where the\n\t// ref will always have a current value that is not null or undefined but it\n\t// needs to start as undefined. We don't want to change the return type so\n\t// it's easier to just ts-ignore this specific line that's complaining about\n\t// undefined not being part of T.\n\t// @ts-ignore\n\tconst ref = useRef();\n\tref.current = value;\n\treturn ref;\n}\n\n/**\n * A hook to facilitate drag and drop handling.\n *\n * @param {Object} props Named parameters.\n * @param {boolean} props.isDisabled Whether or not to disable the drop zone.\n * @param {(e: DragEvent) => void} props.onDragStart Called when dragging has started.\n * @param {(e: DragEvent) => void} props.onDragEnter Called when the zone is entered.\n * @param {(e: DragEvent) => void} props.onDragOver Called when the zone is moved within.\n * @param {(e: DragEvent) => void} props.onDragLeave Called when the zone is left.\n * @param {(e: MouseEvent) => void} props.onDragEnd Called when dragging has ended.\n * @param {(e: DragEvent) => void} props.onDrop Called when dropping in the zone.\n *\n * @return {import('react').RefCallback<HTMLElement>} Ref callback to be passed to the drop zone element.\n */\nexport default function useDropZone( {\n\tisDisabled,\n\tonDrop: _onDrop,\n\tonDragStart: _onDragStart,\n\tonDragEnter: _onDragEnter,\n\tonDragLeave: _onDragLeave,\n\tonDragEnd: _onDragEnd,\n\tonDragOver: _onDragOver,\n} ) {\n\tconst onDropRef = useFreshRef( _onDrop );\n\tconst onDragStartRef = useFreshRef( _onDragStart );\n\tconst onDragEnterRef = useFreshRef( _onDragEnter );\n\tconst onDragLeaveRef = useFreshRef( _onDragLeave );\n\tconst onDragEndRef = useFreshRef( _onDragEnd );\n\tconst onDragOverRef = useFreshRef( _onDragOver );\n\n\treturn useRefEffect(\n\t\t( element ) => {\n\t\t\tif ( isDisabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet isDragging = false;\n\n\t\t\tconst { ownerDocument } = element;\n\n\t\t\t/**\n\t\t\t * Checks if an element is in the drop zone.\n\t\t\t *\n\t\t\t * @param {HTMLElement|null} elementToCheck\n\t\t\t *\n\t\t\t * @return {boolean} True if in drop zone, false if not.\n\t\t\t */\n\t\t\tfunction isElementInZone( elementToCheck ) {\n\t\t\t\tif (\n\t\t\t\t\t! elementToCheck ||\n\t\t\t\t\t! element.contains( elementToCheck )\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\tdo {\n\t\t\t\t\tif ( elementToCheck.dataset.isDropZone ) {\n\t\t\t\t\t\treturn elementToCheck === element;\n\t\t\t\t\t}\n\t\t\t\t} while ( ( elementToCheck = elementToCheck.parentElement ) );\n\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tfunction maybeDragStart( /** @type {DragEvent} */ event ) {\n\t\t\t\tif ( isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = true;\n\n\t\t\t\townerDocument.removeEventListener(\n\t\t\t\t\t'dragenter',\n\t\t\t\t\tmaybeDragStart\n\t\t\t\t);\n\n\t\t\t\t// Note that `dragend` doesn't fire consistently for file and\n\t\t\t\t// HTML drag events where the drag origin is outside the browser\n\t\t\t\t// window. In Firefox it may also not fire if the originating\n\t\t\t\t// node is removed.\n\t\t\t\townerDocument.addEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.addEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( onDragStartRef.current ) {\n\t\t\t\t\tonDragStartRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragEnter( /** @type {DragEvent} */ event ) {\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// The `dragenter` event will also fire when entering child\n\t\t\t\t// elements, but we only want to call `onDragEnter` when\n\t\t\t\t// entering the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been left) should be outside the drop zone.\n\t\t\t\tif (\n\t\t\t\t\telement.contains(\n\t\t\t\t\t\t/** @type {Node} */ ( event.relatedTarget )\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( onDragEnterRef.current ) {\n\t\t\t\t\tonDragEnterRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragOver( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Only call onDragOver for the innermost hovered drop zones.\n\t\t\t\tif ( ! event.defaultPrevented && onDragOverRef.current ) {\n\t\t\t\t\tonDragOverRef.current( event );\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDragOver` is already handled.\n\t\t\t\tevent.preventDefault();\n\t\t\t}\n\n\t\t\tfunction onDragLeave( /** @type {DragEvent} */ event ) {\n\t\t\t\t// The `dragleave` event will also fire when leaving child\n\t\t\t\t// elements, but we only want to call `onDragLeave` when\n\t\t\t\t// leaving the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been entered) should be outside the drop\n\t\t\t\t// zone.\n\t\t\t\tif (\n\t\t\t\t\tisElementInZone(\n\t\t\t\t\t\t/** @type {HTMLElement|null} */ ( event.relatedTarget )\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( onDragLeaveRef.current ) {\n\t\t\t\t\tonDragLeaveRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDrop( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Don't handle drop if an inner drop zone already handled it.\n\t\t\t\tif ( event.defaultPrevented ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDrop` is already handled.\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// This seemingly useless line has been shown to resolve a\n\t\t\t\t// Safari issue where files dragged directly from the dock are\n\t\t\t\t// not recognized.\n\t\t\t\t// eslint-disable-next-line no-unused-expressions\n\t\t\t\tevent.dataTransfer && event.dataTransfer.files.length;\n\n\t\t\t\tif ( onDropRef.current ) {\n\t\t\t\t\tonDropRef.current( event );\n\t\t\t\t}\n\n\t\t\t\tmaybeDragEnd( event );\n\t\t\t}\n\n\t\t\tfunction maybeDragEnd( /** @type {MouseEvent} */ event ) {\n\t\t\t\tif ( ! isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = false;\n\n\t\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( onDragEndRef.current ) {\n\t\t\t\t\tonDragEndRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\telement.dataset.isDropZone = 'true';\n\t\t\telement.addEventListener( 'drop', onDrop );\n\t\t\telement.addEventListener( 'dragenter', onDragEnter );\n\t\t\telement.addEventListener( 'dragover', onDragOver );\n\t\t\telement.addEventListener( 'dragleave', onDragLeave );\n\t\t\t// The `dragstart` event doesn't fire if the drag started outside\n\t\t\t// the document.\n\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\n\t\t\treturn () => {\n\t\t\t\tdelete element.dataset.isDropZone;\n\t\t\t\telement.removeEventListener( 'drop', onDrop );\n\t\t\t\telement.removeEventListener( 'dragenter', onDragEnter );\n\t\t\t\telement.removeEventListener( 'dragover', onDragOver );\n\t\t\t\telement.removeEventListener( 'dragleave', onDragLeave );\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\t\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\t\t\t};\n\t\t},\n\t\t[ isDisabled ]\n\t);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-drop-zone/index.js"],"names":["useFreshRef","value","ref","current","useDropZone","isDisabled","onDrop","_onDrop","onDragStart","_onDragStart","onDragEnter","_onDragEnter","onDragLeave","_onDragLeave","onDragEnd","_onDragEnd","onDragOver","_onDragOver","onDropRef","onDragStartRef","onDragEnterRef","onDragLeaveRef","onDragEndRef","onDragOverRef","element","isDragging","ownerDocument","isElementInZone","targetToCheck","defaultView","HTMLElement","contains","elementToCheck","dataset","isDropZone","parentElement","maybeDragStart","event","removeEventListener","addEventListener","maybeDragEnd","preventDefault","relatedTarget","defaultPrevented","dataTransfer","files","length"],"mappings":";;;;;;;;;AAGA;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,WAAT,CAAsBC,KAAtB,EAA8B;AAC7B;;AACA;;AACA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,QAAMC,GAAG,GAAG,sBAAZ;AACAA,EAAAA,GAAG,CAACC,OAAJ,GAAcF,KAAd;AACA,SAAOC,GAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACe,SAASE,WAAT,CAAsB;AACpCC,EAAAA,UADoC;AAEpCC,EAAAA,MAAM,EAAEC,OAF4B;AAGpCC,EAAAA,WAAW,EAAEC,YAHuB;AAIpCC,EAAAA,WAAW,EAAEC,YAJuB;AAKpCC,EAAAA,WAAW,EAAEC,YALuB;AAMpCC,EAAAA,SAAS,EAAEC,UANyB;AAOpCC,EAAAA,UAAU,EAAEC;AAPwB,CAAtB,EAQX;AACH,QAAMC,SAAS,GAAGlB,WAAW,CAAEO,OAAF,CAA7B;AACA,QAAMY,cAAc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zC;AACAQ,MAAAA,OAAO,CAACc,mBAAR,CAA6B,WAA7B,EAA0C1B,WAA1C;AACAc,MAAAA,aAAa,CAACY,mBAAd,CAAmC,SAAnC,EAA8CE,YAA9C;AACAd,MAAAA,aAAa,CAACY,mBAAd,CAAmC,WAAnC,EAAgDE,YAAhD;AACAd,MAAAA,aAAa,CAACa,gBAAd,CAAgC,WAAhC,EAA6CH,cAA7C;AACA,KATD;AAUA,GAxKK,EAyKN,CAAE/B,UAAF,CAzKM,CAAP;AA2KA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\n\n/* eslint-disable jsdoc/valid-types */\n/**\n * @template T\n * @param {T} value\n * @return {import('react').MutableRefObject<T>} A ref with the value.\n */\nfunction useFreshRef( value ) {\n\t/* eslint-enable jsdoc/valid-types */\n\t/* eslint-disable jsdoc/no-undefined-types */\n\t/** @type {import('react').MutableRefObject<T>} */\n\t/* eslint-enable jsdoc/no-undefined-types */\n\t// Disable reason: We're doing something pretty JavaScript-y here where the\n\t// ref will always have a current value that is not null or undefined but it\n\t// needs to start as undefined. We don't want to change the return type so\n\t// it's easier to just ts-ignore this specific line that's complaining about\n\t// undefined not being part of T.\n\t// @ts-ignore\n\tconst ref = useRef();\n\tref.current = value;\n\treturn ref;\n}\n\n/**\n * A hook to facilitate drag and drop handling.\n *\n * @param {Object} props Named parameters.\n * @param {boolean} props.isDisabled Whether or not to disable the drop zone.\n * @param {(e: DragEvent) => void} props.onDragStart Called when dragging has started.\n * @param {(e: DragEvent) => void} props.onDragEnter Called when the zone is entered.\n * @param {(e: DragEvent) => void} props.onDragOver Called when the zone is moved within.\n * @param {(e: DragEvent) => void} props.onDragLeave Called when the zone is left.\n * @param {(e: MouseEvent) => void} props.onDragEnd Called when dragging has ended.\n * @param {(e: DragEvent) => void} props.onDrop Called when dropping in the zone.\n *\n * @return {import('react').RefCallback<HTMLElement>} Ref callback to be passed to the drop zone element.\n */\nexport default function useDropZone( {\n\tisDisabled,\n\tonDrop: _onDrop,\n\tonDragStart: _onDragStart,\n\tonDragEnter: _onDragEnter,\n\tonDragLeave: _onDragLeave,\n\tonDragEnd: _onDragEnd,\n\tonDragOver: _onDragOver,\n} ) {\n\tconst onDropRef = useFreshRef( _onDrop );\n\tconst onDragStartRef = useFreshRef( _onDragStart );\n\tconst onDragEnterRef = useFreshRef( _onDragEnter );\n\tconst onDragLeaveRef = useFreshRef( _onDragLeave );\n\tconst onDragEndRef = useFreshRef( _onDragEnd );\n\tconst onDragOverRef = useFreshRef( _onDragOver );\n\n\treturn useRefEffect(\n\t\t( element ) => {\n\t\t\tif ( isDisabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet isDragging = false;\n\n\t\t\tconst { ownerDocument } = element;\n\n\t\t\t/**\n\t\t\t * Checks if an element is in the drop zone.\n\t\t\t *\n\t\t\t * @param {EventTarget|null} targetToCheck\n\t\t\t *\n\t\t\t * @return {boolean} True if in drop zone, false if not.\n\t\t\t */\n\t\t\tfunction isElementInZone( targetToCheck ) {\n\t\t\t\tconst { defaultView } = ownerDocument;\n\t\t\t\tif (\n\t\t\t\t\t! targetToCheck ||\n\t\t\t\t\t! defaultView ||\n\t\t\t\t\t! ( targetToCheck instanceof defaultView.HTMLElement ) ||\n\t\t\t\t\t! element.contains( targetToCheck )\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\t/** @type {HTMLElement|null} */\n\t\t\t\tlet elementToCheck = targetToCheck;\n\n\t\t\t\tdo {\n\t\t\t\t\tif ( elementToCheck.dataset.isDropZone ) {\n\t\t\t\t\t\treturn elementToCheck === element;\n\t\t\t\t\t}\n\t\t\t\t} while ( ( elementToCheck = elementToCheck.parentElement ) );\n\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tfunction maybeDragStart( /** @type {DragEvent} */ event ) {\n\t\t\t\tif ( isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = true;\n\n\t\t\t\townerDocument.removeEventListener(\n\t\t\t\t\t'dragenter',\n\t\t\t\t\tmaybeDragStart\n\t\t\t\t);\n\n\t\t\t\t// Note that `dragend` doesn't fire consistently for file and\n\t\t\t\t// HTML drag events where the drag origin is outside the browser\n\t\t\t\t// window. In Firefox it may also not fire if the originating\n\t\t\t\t// node is removed.\n\t\t\t\townerDocument.addEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.addEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( onDragStartRef.current ) {\n\t\t\t\t\tonDragStartRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragEnter( /** @type {DragEvent} */ event ) {\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// The `dragenter` event will also fire when entering child\n\t\t\t\t// elements, but we only want to call `onDragEnter` when\n\t\t\t\t// entering the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been left) should be outside the drop zone.\n\t\t\t\tif (\n\t\t\t\t\telement.contains(\n\t\t\t\t\t\t/** @type {Node} */ ( event.relatedTarget )\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( onDragEnterRef.current ) {\n\t\t\t\t\tonDragEnterRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragOver( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Only call onDragOver for the innermost hovered drop zones.\n\t\t\t\tif ( ! event.defaultPrevented && onDragOverRef.current ) {\n\t\t\t\t\tonDragOverRef.current( event );\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDragOver` is already handled.\n\t\t\t\tevent.preventDefault();\n\t\t\t}\n\n\t\t\tfunction onDragLeave( /** @type {DragEvent} */ event ) {\n\t\t\t\t// The `dragleave` event will also fire when leaving child\n\t\t\t\t// elements, but we only want to call `onDragLeave` when\n\t\t\t\t// leaving the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been entered) should be outside the drop\n\t\t\t\t// zone.\n\t\t\t\tif ( isElementInZone( event.relatedTarget ) ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( onDragLeaveRef.current ) {\n\t\t\t\t\tonDragLeaveRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDrop( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Don't handle drop if an inner drop zone already handled it.\n\t\t\t\tif ( event.defaultPrevented ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDrop` is already handled.\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// This seemingly useless line has been shown to resolve a\n\t\t\t\t// Safari issue where files dragged directly from the dock are\n\t\t\t\t// not recognized.\n\t\t\t\t// eslint-disable-next-line no-unused-expressions\n\t\t\t\tevent.dataTransfer && event.dataTransfer.files.length;\n\n\t\t\t\tif ( onDropRef.current ) {\n\t\t\t\t\tonDropRef.current( event );\n\t\t\t\t}\n\n\t\t\t\tmaybeDragEnd( event );\n\t\t\t}\n\n\t\t\tfunction maybeDragEnd( /** @type {MouseEvent} */ event ) {\n\t\t\t\tif ( ! isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = false;\n\n\t\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( onDragEndRef.current ) {\n\t\t\t\t\tonDragEndRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\telement.dataset.isDropZone = 'true';\n\t\t\telement.addEventListener( 'drop', onDrop );\n\t\t\telement.addEventListener( 'dragenter', onDragEnter );\n\t\t\telement.addEventListener( 'dragover', onDragOver );\n\t\t\telement.addEventListener( 'dragleave', onDragLeave );\n\t\t\t// The `dragstart` event doesn't fire if the drag started outside\n\t\t\t// the document.\n\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\n\t\t\treturn () => {\n\t\t\t\tdelete element.dataset.isDropZone;\n\t\t\t\telement.removeEventListener( 'drop', onDrop );\n\t\t\t\telement.removeEventListener( 'dragenter', onDragEnter );\n\t\t\t\telement.removeEventListener( 'dragover', onDragOver );\n\t\t\t\telement.removeEventListener( 'dragleave', onDragLeave );\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\t\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\t\t\t};\n\t\t},\n\t\t[ isDisabled ]\n\t);\n}\n"]}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useFixedWindowList;
|
|
7
|
+
|
|
8
|
+
var _lodash = require("lodash");
|
|
9
|
+
|
|
10
|
+
var _element = require("@wordpress/element");
|
|
11
|
+
|
|
12
|
+
var _dom = require("@wordpress/dom");
|
|
13
|
+
|
|
14
|
+
var _keycodes = require("@wordpress/keycodes");
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* External dependencies
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* WordPress dependencies
|
|
22
|
+
*/
|
|
23
|
+
const DEFAULT_INIT_WINDOW_SIZE = 30;
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {Object} WPFixedWindowList
|
|
26
|
+
*
|
|
27
|
+
* @property {number} visibleItems Items visible in the current viewport
|
|
28
|
+
* @property {number} start Start index of the window
|
|
29
|
+
* @property {number} end End index of the window
|
|
30
|
+
* @property {(index:number)=>boolean} itemInView Returns true if item is in the window
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {Object} WPFixedWindowListOptions
|
|
35
|
+
*
|
|
36
|
+
* @property {number} [windowOverscan] Renders windowOverscan number of items before and after the calculated visible window.
|
|
37
|
+
* @property {boolean} [useWindowing] When false avoids calculating the window size
|
|
38
|
+
* @property {number} [initWindowSize] Initial window size to use on first render before we can calculate the window size.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* @param {import('react').RefObject<HTMLElement>} elementRef Used to find the closest scroll container that contains element.
|
|
44
|
+
* @param { number } itemHeight Fixed item height in pixels
|
|
45
|
+
* @param { number } totalItems Total items in list
|
|
46
|
+
* @param { WPFixedWindowListOptions } [options] Options object
|
|
47
|
+
* @return {[ WPFixedWindowList, setFixedListWindow:(nextWindow:WPFixedWindowList)=>void]} Array with the fixed window list and setter
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
function useFixedWindowList(elementRef, itemHeight, totalItems, options) {
|
|
51
|
+
var _options$initWindowSi, _options$useWindowing;
|
|
52
|
+
|
|
53
|
+
const initWindowSize = (_options$initWindowSi = options === null || options === void 0 ? void 0 : options.initWindowSize) !== null && _options$initWindowSi !== void 0 ? _options$initWindowSi : DEFAULT_INIT_WINDOW_SIZE;
|
|
54
|
+
const useWindowing = (_options$useWindowing = options === null || options === void 0 ? void 0 : options.useWindowing) !== null && _options$useWindowing !== void 0 ? _options$useWindowing : true;
|
|
55
|
+
const [fixedListWindow, setFixedListWindow] = (0, _element.useState)({
|
|
56
|
+
visibleItems: initWindowSize,
|
|
57
|
+
start: 0,
|
|
58
|
+
end: initWindowSize,
|
|
59
|
+
itemInView:
|
|
60
|
+
/** @type {number} */
|
|
61
|
+
index => {
|
|
62
|
+
return index >= 0 && index <= initWindowSize;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
(0, _element.useLayoutEffect)(() => {
|
|
66
|
+
var _scrollContainer$owne, _scrollContainer$owne2, _scrollContainer$owne3, _scrollContainer$owne4;
|
|
67
|
+
|
|
68
|
+
if (!useWindowing) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const scrollContainer = (0, _dom.getScrollContainer)(elementRef.current);
|
|
73
|
+
|
|
74
|
+
const measureWindow =
|
|
75
|
+
/** @type {boolean | undefined} */
|
|
76
|
+
initRender => {
|
|
77
|
+
var _options$windowOversc;
|
|
78
|
+
|
|
79
|
+
if (!scrollContainer) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const visibleItems = Math.ceil(scrollContainer.clientHeight / itemHeight); // Aim to keep opening list view fast, afterward we can optimize for scrolling
|
|
84
|
+
|
|
85
|
+
const windowOverscan = initRender ? visibleItems : (_options$windowOversc = options === null || options === void 0 ? void 0 : options.windowOverscan) !== null && _options$windowOversc !== void 0 ? _options$windowOversc : visibleItems;
|
|
86
|
+
const firstViewableIndex = Math.floor(scrollContainer.scrollTop / itemHeight);
|
|
87
|
+
const start = Math.max(0, firstViewableIndex - windowOverscan);
|
|
88
|
+
const end = Math.min(totalItems - 1, firstViewableIndex + visibleItems + windowOverscan);
|
|
89
|
+
setFixedListWindow(lastWindow => {
|
|
90
|
+
const nextWindow = {
|
|
91
|
+
visibleItems,
|
|
92
|
+
start,
|
|
93
|
+
end,
|
|
94
|
+
itemInView:
|
|
95
|
+
/** @type {number} */
|
|
96
|
+
index => {
|
|
97
|
+
return start <= index && index <= end;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
if (lastWindow.start !== nextWindow.start || lastWindow.end !== nextWindow.end || lastWindow.visibleItems !== nextWindow.visibleItems) {
|
|
102
|
+
return nextWindow;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return lastWindow;
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
measureWindow(true);
|
|
110
|
+
const debounceMeasureList = (0, _lodash.debounce)(() => {
|
|
111
|
+
measureWindow();
|
|
112
|
+
}, 16);
|
|
113
|
+
scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.addEventListener('scroll', debounceMeasureList);
|
|
114
|
+
scrollContainer === null || scrollContainer === void 0 ? void 0 : (_scrollContainer$owne = scrollContainer.ownerDocument) === null || _scrollContainer$owne === void 0 ? void 0 : (_scrollContainer$owne2 = _scrollContainer$owne.defaultView) === null || _scrollContainer$owne2 === void 0 ? void 0 : _scrollContainer$owne2.addEventListener('resize', debounceMeasureList);
|
|
115
|
+
scrollContainer === null || scrollContainer === void 0 ? void 0 : (_scrollContainer$owne3 = scrollContainer.ownerDocument) === null || _scrollContainer$owne3 === void 0 ? void 0 : (_scrollContainer$owne4 = _scrollContainer$owne3.defaultView) === null || _scrollContainer$owne4 === void 0 ? void 0 : _scrollContainer$owne4.addEventListener('resize', debounceMeasureList);
|
|
116
|
+
return () => {
|
|
117
|
+
var _scrollContainer$owne5, _scrollContainer$owne6;
|
|
118
|
+
|
|
119
|
+
scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.removeEventListener('scroll', debounceMeasureList);
|
|
120
|
+
scrollContainer === null || scrollContainer === void 0 ? void 0 : (_scrollContainer$owne5 = scrollContainer.ownerDocument) === null || _scrollContainer$owne5 === void 0 ? void 0 : (_scrollContainer$owne6 = _scrollContainer$owne5.defaultView) === null || _scrollContainer$owne6 === void 0 ? void 0 : _scrollContainer$owne6.removeEventListener('resize', debounceMeasureList);
|
|
121
|
+
};
|
|
122
|
+
}, [itemHeight, elementRef, totalItems]);
|
|
123
|
+
(0, _element.useLayoutEffect)(() => {
|
|
124
|
+
var _scrollContainer$owne7, _scrollContainer$owne8;
|
|
125
|
+
|
|
126
|
+
if (!useWindowing) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const scrollContainer = (0, _dom.getScrollContainer)(elementRef.current);
|
|
131
|
+
|
|
132
|
+
const handleKeyDown =
|
|
133
|
+
/** @type {KeyboardEvent} */
|
|
134
|
+
event => {
|
|
135
|
+
switch (event.keyCode) {
|
|
136
|
+
case _keycodes.HOME:
|
|
137
|
+
{
|
|
138
|
+
return scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.scrollTo({
|
|
139
|
+
top: 0
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
case _keycodes.END:
|
|
144
|
+
{
|
|
145
|
+
return scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.scrollTo({
|
|
146
|
+
top: totalItems * itemHeight
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
case _keycodes.PAGEUP:
|
|
151
|
+
{
|
|
152
|
+
return scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.scrollTo({
|
|
153
|
+
top: scrollContainer.scrollTop - fixedListWindow.visibleItems * itemHeight
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
case _keycodes.PAGEDOWN:
|
|
158
|
+
{
|
|
159
|
+
return scrollContainer === null || scrollContainer === void 0 ? void 0 : scrollContainer.scrollTo({
|
|
160
|
+
top: scrollContainer.scrollTop + fixedListWindow.visibleItems * itemHeight
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
scrollContainer === null || scrollContainer === void 0 ? void 0 : (_scrollContainer$owne7 = scrollContainer.ownerDocument) === null || _scrollContainer$owne7 === void 0 ? void 0 : (_scrollContainer$owne8 = _scrollContainer$owne7.defaultView) === null || _scrollContainer$owne8 === void 0 ? void 0 : _scrollContainer$owne8.addEventListener('keydown', handleKeyDown);
|
|
167
|
+
return () => {
|
|
168
|
+
var _scrollContainer$owne9, _scrollContainer$owne10;
|
|
169
|
+
|
|
170
|
+
scrollContainer === null || scrollContainer === void 0 ? void 0 : (_scrollContainer$owne9 = scrollContainer.ownerDocument) === null || _scrollContainer$owne9 === void 0 ? void 0 : (_scrollContainer$owne10 = _scrollContainer$owne9.defaultView) === null || _scrollContainer$owne10 === void 0 ? void 0 : _scrollContainer$owne10.removeEventListener('keydown', handleKeyDown);
|
|
171
|
+
};
|
|
172
|
+
}, [totalItems, itemHeight, elementRef, fixedListWindow.visibleItems]);
|
|
173
|
+
return [fixedListWindow, setFixedListWindow];
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-fixed-window-list/index.js"],"names":["DEFAULT_INIT_WINDOW_SIZE","useFixedWindowList","elementRef","itemHeight","totalItems","options","initWindowSize","useWindowing","fixedListWindow","setFixedListWindow","visibleItems","start","end","itemInView","index","scrollContainer","current","measureWindow","initRender","Math","ceil","clientHeight","windowOverscan","firstViewableIndex","floor","scrollTop","max","min","lastWindow","nextWindow","debounceMeasureList","addEventListener","ownerDocument","defaultView","removeEventListener","handleKeyDown","event","keyCode","HOME","scrollTo","top","END","PAGEUP","PAGEDOWN"],"mappings":";;;;;;;AAGA;;AAKA;;AACA;;AACA;;AAVA;AACA;AACA;;AAGA;AACA;AACA;AAKA,MAAMA,wBAAwB,GAAG,EAAjC;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACe,SAASC,kBAAT,CACdC,UADc,EAEdC,UAFc,EAGdC,UAHc,EAIdC,OAJc,EAKb;AAAA;;AACD,QAAMC,cAAc,4BAAGD,OAAH,aAAGA,OAAH,uBAAGA,OAAO,CAAEC,cAAZ,yEAA8BN,wBAAlD;AACA,QAAMO,YAAY,4BAAGF,OAAH,aAAGA,OAAH,uBAAGA,OAAO,CAAEE,YAAZ,yEAA4B,IAA9C;AAEA,QAAM,CAAEC,eAAF,EAAmBC,kBAAnB,IAA0C,uBAAU;AACzDC,IAAAA,YA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E/B,UAAF,EAAcD,UAAd,EAA0BD,UAA1B,EAAsCM,eAAe,CAACE,YAAtD,CAzCH;AA2CA,SAAO,CAAEF,eAAF,EAAmBC,kBAAnB,CAAP;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { debounce } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport { useState, useLayoutEffect } from '@wordpress/element';\nimport { getScrollContainer } from '@wordpress/dom';\nimport { PAGEUP, PAGEDOWN, HOME, END } from '@wordpress/keycodes';\n\nconst DEFAULT_INIT_WINDOW_SIZE = 30;\n\n/**\n * @typedef {Object} WPFixedWindowList\n *\n * @property {number} visibleItems Items visible in the current viewport\n * @property {number} start Start index of the window\n * @property {number} end End index of the window\n * @property {(index:number)=>boolean} itemInView Returns true if item is in the window\n */\n\n/**\n * @typedef {Object} WPFixedWindowListOptions\n *\n * @property {number} [windowOverscan] Renders windowOverscan number of items before and after the calculated visible window.\n * @property {boolean} [useWindowing] When false avoids calculating the window size\n * @property {number} [initWindowSize] Initial window size to use on first render before we can calculate the window size.\n */\n\n/**\n *\n * @param {import('react').RefObject<HTMLElement>} elementRef Used to find the closest scroll container that contains element.\n * @param { number } itemHeight Fixed item height in pixels\n * @param { number } totalItems Total items in list\n * @param { WPFixedWindowListOptions } [options] Options object\n * @return {[ WPFixedWindowList, setFixedListWindow:(nextWindow:WPFixedWindowList)=>void]} Array with the fixed window list and setter\n */\nexport default function useFixedWindowList(\n\telementRef,\n\titemHeight,\n\ttotalItems,\n\toptions\n) {\n\tconst initWindowSize = options?.initWindowSize ?? DEFAULT_INIT_WINDOW_SIZE;\n\tconst useWindowing = options?.useWindowing ?? true;\n\n\tconst [ fixedListWindow, setFixedListWindow ] = useState( {\n\t\tvisibleItems: initWindowSize,\n\t\tstart: 0,\n\t\tend: initWindowSize,\n\t\titemInView: ( /** @type {number} */ index ) => {\n\t\t\treturn index >= 0 && index <= initWindowSize;\n\t\t},\n\t} );\n\n\tuseLayoutEffect( () => {\n\t\tif ( ! useWindowing ) {\n\t\t\treturn;\n\t\t}\n\t\tconst scrollContainer = getScrollContainer( elementRef.current );\n\t\tconst measureWindow = (\n\t\t\t/** @type {boolean | undefined} */ initRender\n\t\t) => {\n\t\t\tif ( ! scrollContainer ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst visibleItems = Math.ceil(\n\t\t\t\tscrollContainer.clientHeight / itemHeight\n\t\t\t);\n\t\t\t// Aim to keep opening list view fast, afterward we can optimize for scrolling\n\t\t\tconst windowOverscan = initRender\n\t\t\t\t? visibleItems\n\t\t\t\t: options?.windowOverscan ?? visibleItems;\n\t\t\tconst firstViewableIndex = Math.floor(\n\t\t\t\tscrollContainer.scrollTop / itemHeight\n\t\t\t);\n\t\t\tconst start = Math.max( 0, firstViewableIndex - windowOverscan );\n\t\t\tconst end = Math.min(\n\t\t\t\ttotalItems - 1,\n\t\t\t\tfirstViewableIndex + visibleItems + windowOverscan\n\t\t\t);\n\t\t\tsetFixedListWindow( ( lastWindow ) => {\n\t\t\t\tconst nextWindow = {\n\t\t\t\t\tvisibleItems,\n\t\t\t\t\tstart,\n\t\t\t\t\tend,\n\t\t\t\t\titemInView: ( /** @type {number} */ index ) => {\n\t\t\t\t\t\treturn start <= index && index <= end;\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t\tif (\n\t\t\t\t\tlastWindow.start !== nextWindow.start ||\n\t\t\t\t\tlastWindow.end !== nextWindow.end ||\n\t\t\t\t\tlastWindow.visibleItems !== nextWindow.visibleItems\n\t\t\t\t) {\n\t\t\t\t\treturn nextWindow;\n\t\t\t\t}\n\t\t\t\treturn lastWindow;\n\t\t\t} );\n\t\t};\n\n\t\tmeasureWindow( true );\n\t\tconst debounceMeasureList = debounce( () => {\n\t\t\tmeasureWindow();\n\t\t}, 16 );\n\t\tscrollContainer?.addEventListener( 'scroll', debounceMeasureList );\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'resize',\n\t\t\tdebounceMeasureList\n\t\t);\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'resize',\n\t\t\tdebounceMeasureList\n\t\t);\n\n\t\treturn () => {\n\t\t\tscrollContainer?.removeEventListener(\n\t\t\t\t'scroll',\n\t\t\t\tdebounceMeasureList\n\t\t\t);\n\t\t\tscrollContainer?.ownerDocument?.defaultView?.removeEventListener(\n\t\t\t\t'resize',\n\t\t\t\tdebounceMeasureList\n\t\t\t);\n\t\t};\n\t}, [ itemHeight, elementRef, totalItems ] );\n\n\tuseLayoutEffect( () => {\n\t\tif ( ! useWindowing ) {\n\t\t\treturn;\n\t\t}\n\t\tconst scrollContainer = getScrollContainer( elementRef.current );\n\t\tconst handleKeyDown = ( /** @type {KeyboardEvent} */ event ) => {\n\t\t\tswitch ( event.keyCode ) {\n\t\t\t\tcase HOME: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( { top: 0 } );\n\t\t\t\t}\n\t\t\t\tcase END: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop: totalItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tcase PAGEUP: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop:\n\t\t\t\t\t\t\tscrollContainer.scrollTop -\n\t\t\t\t\t\t\tfixedListWindow.visibleItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tcase PAGEDOWN: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop:\n\t\t\t\t\t\t\tscrollContainer.scrollTop +\n\t\t\t\t\t\t\tfixedListWindow.visibleItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'keydown',\n\t\t\thandleKeyDown\n\t\t);\n\t\treturn () => {\n\t\t\tscrollContainer?.ownerDocument?.defaultView?.removeEventListener(\n\t\t\t\t'keydown',\n\t\t\t\thandleKeyDown\n\t\t\t);\n\t\t};\n\t}, [ totalItems, itemHeight, elementRef, fixedListWindow.visibleItems ] );\n\n\treturn [ fixedListWindow, setFixedListWindow ];\n}\n"]}
|