-
Notifications
You must be signed in to change notification settings - Fork 1
/
1230-37190b5778ac2fa53be9.js
1 lines (1 loc) · 43.4 KB
/
1230-37190b5778ac2fa53be9.js
1
(self.webpackChunkzent_docs=self.webpackChunkzent_docs||[]).push([[1230],{21230:(n,s,a)=>{"use strict";a.r(s),a.d(s,{default:()=>x});var t=a(73450),p=a(73118),e=a(27378),o=a(57318),c=a(49744),l=a(80186),u=a(14322),i=a(25120),k=a(24246);function r(n,s){var a=Object.keys(n);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(n);s&&(t=t.filter((function(s){return Object.getOwnPropertyDescriptor(n,s).enumerable}))),a.push.apply(a,t)}return a}function d(n){for(var s=1;s<arguments.length;s++){var a=null!=arguments[s]?arguments[s]:{};s%2?r(Object(a),!0).forEach((function(s){(0,t.Z)(n,s,a[s])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(a)):r(Object(a)).forEach((function(s){Object.defineProperty(n,s,Object.getOwnPropertyDescriptor(a,s))}))}return n}var m=function(){function n(){var n=e.useState(0),s=(0,p.Z)(n,2),a=s[0],t=s[1],o=e.useCallback((()=>{t(a+1)}),[a]);return e.useLayoutEffect((()=>(0,c.Oo)(window,"click",o)),[o]),(0,k.jsxs)("div",{children:[(0,k.jsx)("span",{children:"Click on page"}),(0,k.jsx)("span",{style:{marginLeft:"1em"},children:a})]})}return(0,k.jsx)(n,{})},h=function(){function n(){var n=e.useState(0),s=(0,p.Z)(n,2),a=s[0],t=s[1],o=e.useCallback((()=>{t(a+1)}),[a]);return(0,c.xd)(window,"click",o),(0,k.jsxs)("div",{children:[(0,k.jsx)("span",{children:"Click on page"}),(0,k.jsx)("span",{style:{marginLeft:"1em"},children:a})]})}return(0,k.jsx)(n,{})},g=function(){function n(){var n=e.useState(0),s=(0,p.Z)(n,2),a=s[0],t=s[1],o=e.useCallback((()=>{t(a+1)}),[a]);return(0,k.jsxs)("div",{children:[(0,k.jsx)("span",{children:"Click on page"}),(0,k.jsx)("span",{style:{marginLeft:"1em"},children:a}),(0,k.jsx)(c.bi,{target:window,eventName:"click",listener:o})]})}return(0,k.jsx)(n,{})},v=function(){function n(){var n=e.useState(0),s=(0,p.Z)(n,2),a=s[0],t=s[1],o=e.useCallback((()=>{t(a+1)}),[a]);return(0,k.jsxs)("div",{children:[(0,k.jsx)("span",{children:"Click on page"}),(0,k.jsx)("span",{style:{marginLeft:"1em"},children:a}),(0,k.jsx)(l.ZM,{eventName:"click",listener:o})]})}return(0,k.jsx)(n,{})},f=function(){function n(){var n=e.useState(0),s=(0,p.Z)(n,2),a=s[0],t=s[1],o=e.useCallback((()=>{t(a+1)}),[a]);return(0,k.jsxs)("div",{children:[(0,k.jsx)("span",{children:"Try to resize page"}),(0,k.jsx)("span",{style:{marginLeft:"1em"},children:a}),(0,k.jsx)(u.w,{onResize:o})]})}return(0,k.jsx)(n,{})},y=function(){function n(){var n=e.useState(0),s=(0,p.Z)(n,2),a=s[0],t=s[1],o=e.useCallback((()=>{t(a+1)}),[a]);return(0,k.jsxs)("div",{children:[(0,k.jsx)("span",{children:"Try to scroll on page"}),(0,k.jsx)("span",{style:{marginLeft:"1em"},children:a}),(0,k.jsx)(i.f,{onScroll:o})]})}return(0,k.jsx)(n,{})};function w(n){return(0,k.jsx)(n.tag,d(d({},n.attributes),{},{dangerouslySetInnerHTML:{__html:n.html}}))}function b(n){return(0,k.jsx)(w,{tag:"section",html:n.html,attributes:{className:"zandoc-react-markdown"}})}function j(n){return(0,k.jsx)(w,{tag:"style",html:n.style})}function E(n,s){for(;n;)return n.offsetTop&&"static"!==getComputedStyle(n).position&&(s+=n.offsetTop),E(n.parentNode,s);return s}class C extends e.Component{constructor(...n){super(...n),(0,t.Z)(this,"state",{showCode:!1}),(0,t.Z)(this,"toggle",(()=>{this.setState({showCode:!this.state.showCode})}))}render(){var n=this.state.showCode,s=this.props,a=s.title,t=s.src,p=s.children;return(0,k.jsxs)("div",{className:"zandoc-react-demo",children:[(0,k.jsx)("div",{className:"zandoc-react-demo__preview",children:p}),(0,k.jsxs)("div",{className:"zandoc-react-demo__bottom",onClick:this.toggle,children:[(0,k.jsx)("div",{className:"zandoc-react-demo__title",children:(0,k.jsx)("p",{children:a||""})}),(0,k.jsx)("i",{className:"zenticon zenticon-caret-up zandoc-react-demo__toggle ".concat(n?"zandoc-react-demo__toggle-on":"zandoc-react-demo__toggle-off")})]}),n&&(0,k.jsx)("pre",{className:"zandoc-react-demo__code",children:(0,k.jsx)(w,{tag:"code",html:t,attributes:{className:"language-jsx"}})})]})}}class x extends e.Component{componentDidMount(){var n=location.hash;if(n){var s=document.querySelector('a[href="'.concat(n,'"]'));s&&(0,o.l)(document.documentElement,0,E(s,-9))}}render(){return e.createElement("div",{className:"zandoc-react-container"},e.createElement(j,{style:""}),e.createElement(b,{html:'<h2 class="anchor-heading"><a href="#eventhandler">¶</a><a href="javascript:void(0)" id="eventhandler" class="anchor-point"></a>EventHandler</h2>\n<ul>\n<li>Manage browser event handlers with different APIS: functions, hooks or React components</li>\n<li>Multiple event handlers are consolidated</li>\n<li>Useful on global event handlers and dynamically attach event handlers</li>\n<li>Use React\'s native event system if you can, use this only when you\'re out of luck with React</li>\n<li>Provide dedicated components for event handlers on <code>window</code></li>\n</ul>\n<h3 class="anchor-heading"><a href="#demos">¶</a><a href="javascript:void(0)" id="demos" class="anchor-point"></a>Demos</h3>'}),e.createElement(C,{title:"addEventListener function",id:"Demo01basic",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> addEventListener <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">function</span> <span class="token function"><span class="token maybe-class-name">Demo</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword">const</span> <span class="token punctuation">[</span>count<span class="token punctuation">,</span> setCount<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useState</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token keyword">const</span> onPageClick <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useCallback</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token function">setCount</span><span class="token punctuation">(</span>count <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useLayoutEffect</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">return</span> <span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token dom variable">window</span><span class="token punctuation">,</span> <span class="token string">\'click\'</span><span class="token punctuation">,</span> onPageClick<span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>onPageClick<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span><span class="token punctuation">></span></span><span class="token maybe-class-name">Click</span> on page<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">marginLeft</span><span class="token operator">:</span> <span class="token string">\'1em\'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>count<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Demo</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">,</span> mountNode<span class="token punctuation">)</span><span class="token punctuation">;</span>'},e.createElement(m)),e.createElement(C,{title:"useEventHandler Hook",id:"Demo02hook",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> useEventHandler <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">function</span> <span class="token function"><span class="token maybe-class-name">Demo</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword">const</span> <span class="token punctuation">[</span>count<span class="token punctuation">,</span> setCount<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useState</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token keyword">const</span> onPageClick <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useCallback</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token function">setCount</span><span class="token punctuation">(</span>count <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n <span class="token function">useEventHandler</span><span class="token punctuation">(</span><span class="token dom variable">window</span><span class="token punctuation">,</span> <span class="token string">\'click\'</span><span class="token punctuation">,</span> onPageClick<span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span><span class="token punctuation">></span></span><span class="token maybe-class-name">Click</span> on page<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">marginLeft</span><span class="token operator">:</span> <span class="token string">\'1em\'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>count<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Demo</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">,</span> mountNode<span class="token punctuation">)</span><span class="token punctuation">;</span>'},e.createElement(h)),e.createElement(C,{title:"EventHandler component",id:"Demo03eventhandler",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">EventHandler</span> <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">function</span> <span class="token function"><span class="token maybe-class-name">Demo</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword">const</span> <span class="token punctuation">[</span>count<span class="token punctuation">,</span> setCount<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useState</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token keyword">const</span> onPageClick <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useCallback</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token function">setCount</span><span class="token punctuation">(</span>count <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span><span class="token punctuation">></span></span><span class="token maybe-class-name">Click</span> on page<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">marginLeft</span><span class="token operator">:</span> <span class="token string">\'1em\'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>count<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">EventHandler</span></span> <span class="token attr-name">target</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token dom variable">window</span><span class="token punctuation">}</span></span> <span class="token attr-name">eventName</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>click<span class="token punctuation">"</span></span> <span class="token attr-name">listener</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>onPageClick<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Demo</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">,</span> mountNode<span class="token punctuation">)</span><span class="token punctuation">;</span>'},e.createElement(g)),e.createElement(C,{title:"WindowEventHandler component",id:"Demo04windoweventhandler",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">WindowEventHandler</span> <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">function</span> <span class="token function"><span class="token maybe-class-name">Demo</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword">const</span> <span class="token punctuation">[</span>count<span class="token punctuation">,</span> setCount<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useState</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token keyword">const</span> onPageClick <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useCallback</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token function">setCount</span><span class="token punctuation">(</span>count <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span><span class="token punctuation">></span></span><span class="token maybe-class-name">Click</span> on page<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">marginLeft</span><span class="token operator">:</span> <span class="token string">\'1em\'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>count<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">WindowEventHandler</span></span> <span class="token attr-name">eventName</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>click<span class="token punctuation">"</span></span> <span class="token attr-name">listener</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>onPageClick<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Demo</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">,</span> mountNode<span class="token punctuation">)</span><span class="token punctuation">;</span>'},e.createElement(v)),e.createElement(C,{title:"WindowResizeHandler component",id:"Demo05windowresizehandler",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">WindowResizeHandler</span> <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">function</span> <span class="token function"><span class="token maybe-class-name">Demo</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword">const</span> <span class="token punctuation">[</span>count<span class="token punctuation">,</span> setCount<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useState</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token keyword">const</span> onPageResize <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useCallback</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token function">setCount</span><span class="token punctuation">(</span>count <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span><span class="token punctuation">></span></span><span class="token maybe-class-name">Try</span> to resize page<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">marginLeft</span><span class="token operator">:</span> <span class="token string">\'1em\'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>count<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">WindowResizeHandler</span></span> <span class="token attr-name">onResize</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>onPageResize<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Demo</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">,</span> mountNode<span class="token punctuation">)</span><span class="token punctuation">;</span>'},e.createElement(f)),e.createElement(C,{title:"WindowScrollHandler component",id:"Demo06windowscrollhandler",src:'<span class="token keyword module">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">WindowScrollHandler</span> <span class="token punctuation">}</span></span> <span class="token keyword module">from</span> <span class="token string">\'zent\'</span><span class="token punctuation">;</span>\n\n<span class="token keyword">function</span> <span class="token function"><span class="token maybe-class-name">Demo</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword">const</span> <span class="token punctuation">[</span>count<span class="token punctuation">,</span> setCount<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useState</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token keyword">const</span> onPageScroll <span class="token operator">=</span> <span class="token maybe-class-name">React</span><span class="token punctuation">.</span><span class="token method function property-access">useCallback</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>\n <span class="token function">setCount</span><span class="token punctuation">(</span>count <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n <span class="token keyword control-flow">return</span> <span class="token punctuation">(</span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span><span class="token punctuation">></span></span><span class="token maybe-class-name">Try</span> to scroll on page<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">style</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token literal-property property">marginLeft</span><span class="token operator">:</span> <span class="token string">\'1em\'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span><span class="token punctuation">{</span>count<span class="token punctuation">}</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">WindowScrollHandler</span></span> <span class="token attr-name">onScroll</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span>onPageScroll<span class="token punctuation">}</span></span> <span class="token punctuation">/></span></span>\n <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>\n <span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n\n<span class="token maybe-class-name">ReactDOM</span><span class="token punctuation">.</span><span class="token method function property-access">render</span><span class="token punctuation">(</span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Demo</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">,</span> mountNode<span class="token punctuation">)</span><span class="token punctuation">;</span>'},e.createElement(y)),e.createElement(b,{html:'<h3 class="anchor-heading"><a href="#api">¶</a><a href="javascript:void(0)" id="api" class="anchor-point"></a>API</h3>\n<p>There\'re several ways to start with.</p>\n<h4 class="anchor-heading"><a href="#addeventlistener-function">¶</a><a href="javascript:void(0)" id="addeventlistener-function" class="anchor-point"></a><code>addEventListener</code> function</h4>\n<p>This is the tranditional way to manage event handlers, just like native <code>addEventListerner</code> function.</p>\n<p>Our <code>addEventListener</code> function returns an unsubscribe function, so there\'s no <code>removeEventListener</code> function.</p>\n<pre><code class="language-ts"><span class="token keyword">function</span> <span class="token generic-function"><span class="token function">addEventListener</span><span class="token generic class-name"><span class="token operator"><</span><span class="token constant">T</span> <span class="token keyword">extends</span> EventTarget <span class="token operator">=</span> HTMLElement<span class="token operator">></span></span></span><span class="token punctuation">(</span>\n target<span class="token operator">:</span> <span class="token constant">T</span><span class="token punctuation">,</span>\n eventName<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span>\n listener<span class="token operator">:</span> EventListener<span class="token punctuation">,</span>\n options<span class="token operator">?</span><span class="token operator">:</span> AddEventListenerOptions\n<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">void</span><span class="token punctuation">;</span></code></pre>\n<h4 class="anchor-heading"><a href="#useeventhandler-hook">¶</a><a href="javascript:void(0)" id="useeventhandler-hook" class="anchor-point"></a><code>useEventHandler</code> Hook</h4>\n<p>A hook encapsultes all the logic for add/remove event handlers, useful if the lifecycle of your event handler is the same as your component.</p>\n<pre><code class="language-ts"><span class="token keyword">function</span> <span class="token generic-function"><span class="token function">useEventHandler</span><span class="token generic class-name"><span class="token operator"><</span>\n <span class="token constant">E</span> <span class="token keyword">extends</span> Event<span class="token punctuation">,</span>\n <span class="token constant">N</span> <span class="token keyword">extends</span> <span class="token builtin">string</span><span class="token punctuation">,</span>\n <span class="token constant">T</span> <span class="token keyword">extends</span> EventTarget <span class="token operator">=</span> HTMLElement\n<span class="token operator">></span></span></span><span class="token punctuation">(</span>\n target<span class="token operator">:</span> <span class="token constant">T</span><span class="token punctuation">,</span>\n eventName<span class="token operator">:</span> <span class="token constant">N</span><span class="token punctuation">,</span>\n <span class="token function-variable function">listener</span><span class="token operator">:</span> <span class="token punctuation">(</span>event<span class="token operator">:</span> <span class="token constant">E</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">void</span><span class="token punctuation">,</span>\n options<span class="token operator">?</span><span class="token operator">:</span> AddEventListenerOptions\n<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span></code></pre>\n<h4 class="anchor-heading"><a href="#eventhandler-component">¶</a><a href="javascript:void(0)" id="eventhandler-component" class="anchor-point"></a><code>EventHandler</code> component</h4>\n<p>A React component encapsulates all the logic for add/remove event handlers, just like <code>useEventHandler</code> hook but in a component way.</p>\n<table class="table">\n<thead>\n<tr>\n<th>Property</th>\n<th>Description</th>\n<th>Type</th>\n<th>Required</th>\n<th>Default</th>\n<th>Alternative</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>target</td>\n<td>Target node to attach event</td>\n<td><code>EventTarget</code></td>\n<td>Yes</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>eventName</td>\n<td>Event to listen to</td>\n<td><code>string</code></td>\n<td>Yes</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>listener</td>\n<td>Event callback</td>\n<td><code>function</code></td>\n<td>Yes</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>options</td>\n<td>Event handler options</td>\n<td><code>AddEventListenerOptions</code></td>\n<td>No</td>\n<td><code>{once: false, capture: false, passive: false}</code></td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<p>There\'re three more components specifically designed for handling events on <code>window</code>.</p>\n<h4 class="anchor-heading"><a href="#windoweventhandler-component">¶</a><a href="javascript:void(0)" id="windoweventhandler-component" class="anchor-point"></a>WindowEventHandler component</h4>\n<table class="table">\n<thead>\n<tr>\n<th>Property</th>\n<th>Description</th>\n<th>Type</th>\n<th>Required</th>\n<th>Default</th>\n<th>Alternative</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>eventName</td>\n<td>Event to listen to</td>\n<td><code>keyof WindowEventMap</code></td>\n<td>Yes</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>listener</td>\n<td>Event callback</td>\n<td><code>(ev: WindowEventMap[K]) => void</code></td>\n<td>Yes</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>options</td>\n<td>Event handler options</td>\n<td><code>AddEventListenerOptions</code></td>\n<td>No</td>\n<td><code>{ once: false, capture: false, passive: false }</code></td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<h4 class="anchor-heading"><a href="#windowresizehandler-component">¶</a><a href="javascript:void(0)" id="windowresizehandler-component" class="anchor-point"></a>WindowResizeHandler component</h4>\n<p><code>onResize</code> callback is already throttled inside this component, it only gets called once per browser render frame.</p>\n<table class="table">\n<thead>\n<tr>\n<th>Property</th>\n<th>Description</th>\n<th>Type</th>\n<th>Required</th>\n<th>Default</th>\n<th>Alternative</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>onResize</td>\n<td>Callback when window resizes</td>\n<td><code>(e: UIEvent, delta: IWindowResizeHandlerDelta) => void</code></td>\n<td>Yes</td>\n<td></td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<h4 class="anchor-heading"><a href="#windowscrollhandler-component">¶</a><a href="javascript:void(0)" id="windowscrollhandler-component" class="anchor-point"></a>WindowScrollHandler component</h4>\n<p><code>onScroll</code> callback is already throttled inside this component, it only gets called once per browser render frame.</p>\n<table class="table">\n<thead>\n<tr>\n<th>Property</th>\n<th>Description</th>\n<th>Type</th>\n<th>Required</th>\n<th>Default</th>\n<th>Alternative</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>onScroll</td>\n<td>Callback when page scrolls</td>\n<td><code>(event: UIEvent) => void</code></td>\n<td>Yes</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>options</td>\n<td>Event handler options</td>\n<td><code>AddEventListenerOptions</code></td>\n<td><code>{ once: false, capture: false, passive: true }</code></td>\n<td></td>\n<td></td>\n</tr>\n</tbody>\n</table>'}))}}},14322:(n,s,a)=>{"use strict";a.d(s,{w:()=>u});var t=a(24246),p=a(27378),e=a(27468),o=a(80186),c=a(17127),l={passive:!0},u=function(n){var s=n.disableThrottle,a=void 0!==s&&s,u=n.onResize,i=(0,p.useRef)(null),k=(0,p.useRef)(u);k.current=u;var r=(0,p.useCallback)((function(n){var s=(0,e.ZP)();i.current||(i.current=s);var a=i.current,t={deltaX:s.width-a.width,deltaY:s.height-a.height};0===t.deltaX&&0===t.deltaY||(k.current(n,t),i.current=s)}),[]),d=(0,c.BN)(r,a);return(0,p.useEffect)((function(){return i.current=(0,e.ZP)(),d.cancel}),[d]),(0,t.jsx)(o.ZP,{eventName:"resize",listener:d,options:l},void 0)}},25120:(n,s,a)=>{"use strict";a.d(s,{f:()=>u});var t=a(59312),p=a(24246),e=a(27378),o=a(80186),c=a(17127),l={passive:!0},u=function(n){var s=n.disableThrottle,a=void 0!==s&&s,u=n.options,i=n.onScroll,k=(0,e.useRef)(i);k.current=i;var r=(0,e.useCallback)((function(n){k.current(n)}),[]),d=(0,c.BN)(r,a);return(0,e.useEffect)((function(){return d.cancel}),[d]),(0,p.jsx)(o.ZM,{eventName:"scroll",listener:d,options:(0,t.pi)((0,t.pi)({},l),u)},void 0)}}}]);