kempo-ui 0.0.10 → 0.0.11
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/docs/components/accordion.html +33 -121
- package/docs/components/card.html +42 -56
- package/docs/components/collapsible.html +37 -157
- package/docs/components/content-slider.html +35 -181
- package/docs/components/dialog.html +37 -302
- package/docs/components/focus-capture.html +41 -77
- package/docs/components/hybrid-component.html +35 -71
- package/docs/components/icon.html +42 -121
- package/docs/components/import.html +42 -38
- package/docs/components/light-component.html +36 -59
- package/docs/components/persistant-collapsible.html +40 -67
- package/docs/components/photo-viewer.html +38 -231
- package/docs/components/resize.html +39 -139
- package/docs/components/shadow-component.html +40 -52
- package/docs/components/show-more.html +41 -91
- package/docs/components/side-menu.html +39 -121
- package/docs/components/sortable.html +35 -92
- package/docs/components/split.html +40 -111
- package/docs/components/table.html +32 -268
- package/docs/components/tableControls.html +52 -53
- package/docs/components/tableCustomFields.html +52 -89
- package/docs/components/tableFetchRecords.html +50 -88
- package/docs/components/tableFieldSortHide.html +52 -51
- package/docs/components/tablePagination.html +52 -76
- package/docs/components/tableRecordEditing.html +52 -120
- package/docs/components/tableRecordFiltering.html +50 -62
- package/docs/components/tableRecordHiding.html +51 -55
- package/docs/components/tableRecordSearching.html +52 -51
- package/docs/components/tableRecordSelection.html +53 -52
- package/docs/components/tableRowControls.html +53 -55
- package/docs/components/tableSorting.html +52 -49
- package/docs/components/tabs.html +43 -165
- package/docs/components/tags.html +39 -101
- package/docs/components/theme-switcher.html +36 -84
- package/docs/components/timestamp.html +39 -74
- package/docs/components/toast.html +29 -316
- package/docs/components/toggle.html +41 -118
- package/docs/components/tree.html +40 -157
- package/docs/index.html +56 -209
- package/docs/utils/debounce.html +32 -15
- package/docs/utils/drag.html +30 -33
- package/docs/utils/formatTimestamp.html +29 -49
- package/docs/utils/init.js +1 -0
- package/docs/utils/propConverters.html +24 -41
- package/docs/utils/toTitleCase.html +29 -24
- package/docs/utils/watchWindowSize.html +1 -0
- package/package.json +1 -1
|
@@ -3,157 +3,69 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
-
<title>
|
|
7
|
-
<link rel="stylesheet" href="../kempo-vars.css"
|
|
6
|
+
<title>watchWindowSize - Kempo UI</title>
|
|
7
|
+
<link rel="stylesheet" href="../kempo-vars.css" />
|
|
8
|
+
<link rel="stylesheet" href="../kempo.min.css" />
|
|
8
9
|
<link rel="stylesheet" href="../kempo-hljs.css" />
|
|
9
10
|
<link rel="stylesheet" href="../styles.css" />
|
|
11
|
+
<script>window.litDisableBundleWarning = true;</script>
|
|
10
12
|
<script type="module" src="./init.js"></script>
|
|
11
13
|
</head>
|
|
12
14
|
<body>
|
|
13
15
|
<k-import src="../nav.inc.html"></k-import>
|
|
14
|
-
<h1 class="ta-center">Accordion</h1>
|
|
15
16
|
<main>
|
|
17
|
+
<h1>watchWindowSize</h1>
|
|
18
|
+
|
|
16
19
|
<details
|
|
17
20
|
class="b r mb"
|
|
18
21
|
>
|
|
19
22
|
<summary class="p">Table of Contents</summary>
|
|
20
23
|
<div class="m mt0 pl">
|
|
21
|
-
<h6>
|
|
24
|
+
<h6>Usage</h6>
|
|
22
25
|
<a href="#basicUsage">Basic Usage</a><br />
|
|
23
|
-
<a href="#
|
|
24
|
-
<
|
|
25
|
-
<a href="#constructor">Constructor</a><br />
|
|
26
|
-
<a href="#requirements">Requirements</a><br />
|
|
27
|
-
<a href="#properties">Properties</a><br />
|
|
28
|
-
<a href="#methods">Methods</a><br />
|
|
29
|
-
<a href="#events">Events</a><br />
|
|
26
|
+
<a href="#functions">Functions</a><br />
|
|
27
|
+
<a href="#examples">Examples</a><br />
|
|
30
28
|
</div>
|
|
31
29
|
</details>
|
|
32
30
|
|
|
33
31
|
<h3>Description</h3>
|
|
34
|
-
<p>
|
|
32
|
+
<p>The <code>watchWindowSize</code> utility provides a debounced way to monitor window resize events. It manages multiple handlers efficiently and automatically debounces resize events to improve performance.</p>
|
|
35
33
|
|
|
36
34
|
<h3 id="basicUsage"><a href="#basicUsage" class="no-link">Basic Usage</a></h3>
|
|
37
|
-
<
|
|
38
|
-
<
|
|
39
|
-
class="mb"
|
|
40
|
-
>
|
|
41
|
-
<k-accordion-header for-panel="panel1">Panel 1</k-accordion-header>
|
|
42
|
-
<k-accordion-panel name="panel1">
|
|
43
|
-
<p class="m">Panel 1 content</p>
|
|
44
|
-
</k-accordion-panel>
|
|
45
|
-
<k-accordion-header for-panel="panel2">Panel 2</k-accordion-header>
|
|
46
|
-
<k-accordion-panel name="panel2">
|
|
47
|
-
<p class="m">Panel 2 content</p>
|
|
48
|
-
</k-accordion-panel>
|
|
49
|
-
<k-accordion-header for-panel="panel3">Panel 3</k-accordion-header>
|
|
50
|
-
<k-accordion-panel name="panel3">
|
|
51
|
-
<p class="m">Panel 3 content</p>
|
|
52
|
-
</k-accordion-panel>
|
|
53
|
-
</k-accordion>
|
|
54
|
-
|
|
55
|
-
<h3 id="defaultOpen"><a href="#defaultOpen" class="no-link">Default Open Panel</a></h3>
|
|
56
|
-
<p>You can have a panel open by default by setting the <code>active</code> attribute to <code>true</code> on both the header and the panel.</p>
|
|
57
|
-
<pre><code class="hljs xml"><span class="hljs-tag"><<span class="hljs-name">k-accordion</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">k-accordion-header</span> <span class="hljs-attr">for-panel</span>=<span class="hljs-string">"panel1"</span> <span class="hljs-attr">active</span>=<span class="hljs-string">"true"</span>></span>Panel 1<span class="hljs-tag"></<span class="hljs-name">k-accordion-header</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">k-accordion-panel</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"panel1"</span> <span class="hljs-attr">active</span>=<span class="hljs-string">"true"</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"m"</span>></span>Panel 1 content<span class="hljs-tag"></<span class="hljs-name">p</span>></span><br /> <span class="hljs-tag"></<span class="hljs-name">k-accordion-panel</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">k-accordion-header</span> <span class="hljs-attr">for-panel</span>=<span class="hljs-string">"panel2"</span>></span>Panel 2<span class="hljs-tag"></<span class="hljs-name">k-accordion-header</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">k-accordion-panel</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"panel2"</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"m"</span>></span>Panel 2 content<span class="hljs-tag"></<span class="hljs-name">p</span>></span><br /> <span class="hljs-tag"></<span class="hljs-name">k-accordion-panel</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">k-accordion-header</span> <span class="hljs-attr">for-panel</span>=<span class="hljs-string">"panel3"</span>></span>Panel 3<span class="hljs-tag"></<span class="hljs-name">k-accordion-header</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">k-accordion-panel</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"panel3"</span>></span><br /> <span class="hljs-tag"><<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"m"</span>></span>Panel 3 content<span class="hljs-tag"></<span class="hljs-name">p</span>></span><br /> <span class="hljs-tag"></<span class="hljs-name">k-accordion-panel</span>></span><br /><span class="hljs-tag"></<span class="hljs-name">k-accordion</span>></span></code></pre>
|
|
58
|
-
<k-accordion
|
|
59
|
-
class="mb"
|
|
60
|
-
>
|
|
61
|
-
<k-accordion-header for-panel="panel1" active="true">Panel 1</k-accordion-header>
|
|
62
|
-
<k-accordion-panel name="panel1" active="true">
|
|
63
|
-
<p class="m">Panel 1 content</p>
|
|
64
|
-
</k-accordion-panel>
|
|
65
|
-
<k-accordion-header for-panel="panel2">Panel 2</k-accordion-header>
|
|
66
|
-
<k-accordion-panel name="panel2">
|
|
67
|
-
<p class="m">Panel 2 content</p>
|
|
68
|
-
</k-accordion-panel>
|
|
69
|
-
<k-accordion-header for-panel="panel3">Panel 3</k-accordion-header>
|
|
70
|
-
<k-accordion-panel name="panel3">
|
|
71
|
-
<p class="m">Panel 3 content</p>
|
|
72
|
-
</k-accordion-panel>
|
|
73
|
-
</k-accordion>
|
|
74
|
-
|
|
75
|
-
<h2 id="jsRef">JavaScript Reference</h2>
|
|
76
|
-
|
|
77
|
-
<h3 id="constructor"><a href="#constructor" class="no-link">Constructor</a></h3>
|
|
78
|
-
<h6>Extends <a href="./component.html">ShadowComponent</a></h6>
|
|
79
|
-
<h5>
|
|
80
|
-
<code>new Accordion()</code><br />
|
|
81
|
-
</h5>
|
|
82
|
-
|
|
83
|
-
<h3 id="requirements"><a href="#requirements" class="no-link">Requirements</a></h3>
|
|
84
|
-
<ul>
|
|
85
|
-
<li><a href="./component.html">ShadowComponent</a></li>
|
|
86
|
-
</ul>
|
|
87
|
-
|
|
88
|
-
<h3 id="properties"><a href="#properties" class="no-link">Properties</a></h3>
|
|
89
|
-
|
|
90
|
-
<h4>Accordion (Main Component)</h4>
|
|
91
|
-
<h5><code>activeHeader<i>: HTMLElement | null</i></code></h5>
|
|
92
|
-
<p>Returns the currently active header element, or <code>null</code> if no panel is open.</p>
|
|
93
|
-
<h5><code>activePanel<i>: HTMLElement | null</i></code></h5>
|
|
94
|
-
<p>Returns the currently active panel element, or <code>null</code> if no panel is open.</p>
|
|
35
|
+
<p>Import and use the window size watching functions:</p>
|
|
36
|
+
<pre><code class="hljs javascript"><span class="hljs-keyword">import</span> { watchWindowSize, unwatchWindowSize } <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/watchWindowSize.js'</span>;<br /><br /><span class="hljs-keyword">const</span> handler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Window width:'</span>, width);<br />};<br /><br /><span class="hljs-comment">// Start watching</span><br /><span class="hljs-keyword">const</span> currentWidth = watchWindowSize(handler);<br /><br /><span class="hljs-comment">// Stop watching when done</span><br />unwatchWindowSize(handler);</code></pre>
|
|
95
37
|
|
|
96
|
-
<
|
|
97
|
-
<h5><code>forPanel<i>: string</i></code></h5>
|
|
98
|
-
<p>The panel name that this header controls. Syncs with the <code>for-panel</code> attribute.</p>
|
|
99
|
-
<h5><code>active<i>: boolean</i></code></h5>
|
|
100
|
-
<p>Whether this header is currently active (its associated panel is open). Syncs with the <code>active</code> attribute.</p>
|
|
101
|
-
<h5><code>accordion<i>: Accordion | null</i></code></h5>
|
|
102
|
-
<p>Returns the parent accordion component, or <code>null</code> if not inside an accordion.</p>
|
|
103
|
-
|
|
104
|
-
<h4>AccordionPanel</h4>
|
|
105
|
-
<h5><code>name<i>: string</i></code></h5>
|
|
106
|
-
<p>Unique identifier for this panel. Must match the <code>for-panel</code> attribute of its associated header. Syncs with the <code>name</code> attribute.</p>
|
|
107
|
-
<h5><code>active<i>: boolean</i></code></h5>
|
|
108
|
-
<p>Whether this panel is currently open. Syncs with the <code>active</code> attribute.</p>
|
|
109
|
-
<h5><code>transitioning<i>: boolean</i></code></h5>
|
|
110
|
-
<p>Whether this panel is currently animating (opening or closing). Syncs with the <code>transitioning</code> attribute.</p>
|
|
111
|
-
<h5><code>accordion<i>: Accordion | null</i></code></h5>
|
|
112
|
-
<p>Returns the parent accordion component, or <code>null</code> if not inside an accordion.</p>
|
|
113
|
-
|
|
114
|
-
<h3 id="methods"><a href="#methods" class="no-link">Methods</a></h3>
|
|
38
|
+
<h3 id="functions"><a href="#functions" class="no-link">Functions</a></h3>
|
|
115
39
|
|
|
116
|
-
<
|
|
117
|
-
<
|
|
118
|
-
<p>Returns the header element associated with the specified panel name, or <code>null</code> if not found.</p>
|
|
40
|
+
<h5><code>watchWindowSize(handler)</code></h5>
|
|
41
|
+
<p>Registers a handler function to be called when the window is resized.</p>
|
|
119
42
|
<ul>
|
|
120
|
-
<li><strong>
|
|
43
|
+
<li><strong>handler:</strong> Function that receives the new window width</li>
|
|
44
|
+
<li><strong>Returns:</strong> Current window width</li>
|
|
121
45
|
</ul>
|
|
122
46
|
|
|
123
|
-
<h5><code>
|
|
124
|
-
<p>
|
|
47
|
+
<h5><code>unwatchWindowSize(handler)</code></h5>
|
|
48
|
+
<p>Unregisters a previously registered handler function.</p>
|
|
125
49
|
<ul>
|
|
126
|
-
<li><strong>
|
|
50
|
+
<li><strong>handler:</strong> The same function reference that was registered</li>
|
|
127
51
|
</ul>
|
|
128
52
|
|
|
129
|
-
<
|
|
130
|
-
|
|
131
|
-
<
|
|
132
|
-
|
|
133
|
-
</ul>
|
|
53
|
+
<h3 id="examples"><a href="#examples" class="no-link">Examples</a></h3>
|
|
54
|
+
|
|
55
|
+
<h4>Responsive Component</h4>
|
|
56
|
+
<pre><code class="hljs javascript"><span class="hljs-keyword">import</span> { watchWindowSize, unwatchWindowSize } <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/watchWindowSize.js'</span>;<br /><br /><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ResponsiveComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LitElement</span> </span>{<br /> <span class="hljs-keyword">constructor</span>() {<br /> <span class="hljs-keyword">super</span>();<br /> <span class="hljs-keyword">this</span>.isMobile = <span class="hljs-literal">false</span>;<br /> <span class="hljs-keyword">this</span>.resizeHandler = <span class="hljs-keyword">this</span>.handleResize.bind(<span class="hljs-keyword">this</span>);<br /> }<br /><br /> connectedCallback() {<br /> <span class="hljs-keyword">super</span>.connectedCallback();<br /> <span class="hljs-keyword">const</span> currentWidth = watchWindowSize(<span class="hljs-keyword">this</span>.resizeHandler);<br /> <span class="hljs-keyword">this</span>.handleResize(currentWidth);<br /> }<br /><br /> disconnectedCallback() {<br /> <span class="hljs-keyword">super</span>.disconnectedCallback();<br /> unwatchWindowSize(<span class="hljs-keyword">this</span>.resizeHandler);<br /> }<br /><br /> handleResize(width) {<br /> <span class="hljs-keyword">this</span>.isMobile = width < <span class="hljs-number">768</span>;<br /> <span class="hljs-keyword">this</span>.requestUpdate();<br /> }<br /><br /> render() {<br /> <span class="hljs-keyword">return</span> html`<span class="xml"><br /> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"</span></span></span><span class="hljs-subst">${<span class="hljs-keyword">this</span>.isMobile ? <span class="hljs-string">'mobile'</span> : <span class="hljs-string">'desktop'</span>}</span><span class="xml"><span class="hljs-tag"><span class="hljs-string">"</span>></span><br /> Content adapts to screen size<br /> <span class="hljs-tag"></<span class="hljs-name">div</span>></span><br /> `</span>;<br /> }<br />}</code></pre>
|
|
134
57
|
|
|
135
|
-
<
|
|
136
|
-
<
|
|
137
|
-
<ul>
|
|
138
|
-
<li><strong>panelName</strong> <code>string</code> - The name of the panel to close</li>
|
|
139
|
-
</ul>
|
|
58
|
+
<h4>Multiple Handlers</h4>
|
|
59
|
+
<pre><code class="hljs javascript"><span class="hljs-comment">// Handler for navigation</span><br /><span class="hljs-keyword">const</span> navHandler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-keyword">const</span> nav = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'.navigation'</span>);<br /> nav.classList.toggle(<span class="hljs-string">'mobile'</span>, width < <span class="hljs-number">1024</span>);<br />};<br /><br /><span class="hljs-comment">// Handler for sidebar</span><br /><span class="hljs-keyword">const</span> sidebarHandler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-keyword">const</span> sidebar = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'.sidebar'</span>);<br /> sidebar.style.display = width < <span class="hljs-number">768</span> ? <span class="hljs-string">'none'</span> : <span class="hljs-string">'block'</span>;<br />};<br /><br /><span class="hljs-comment">// Both handlers will be called on resize</span><br />watchWindowSize(navHandler);<br />watchWindowSize(sidebarHandler);<br /><br /><span class="hljs-comment">// Clean up when needed</span><br />unwatchWindowSize(navHandler);<br />unwatchWindowSize(sidebarHandler);</code></pre>
|
|
140
60
|
|
|
141
|
-
<
|
|
142
|
-
<
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
61
|
+
<h4>Breakpoint Detection</h4>
|
|
62
|
+
<pre><code class="hljs javascript"><span class="hljs-keyword">const</span> breakpoints = {<br /> <span class="hljs-attr">mobile</span>: <span class="hljs-number">768</span>,<br /> <span class="hljs-attr">tablet</span>: <span class="hljs-number">1024</span>,<br /> <span class="hljs-attr">desktop</span>: <span class="hljs-number">1200</span><br />};<br /><br /><span class="hljs-keyword">const</span> breakpointHandler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-keyword">let</span> currentBreakpoint = <span class="hljs-string">'mobile'</span>;<br /> <br /> <span class="hljs-keyword">if</span> (width >= breakpoints.desktop) {<br /> currentBreakpoint = <span class="hljs-string">'desktop'</span>;<br /> } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (width >= breakpoints.tablet) {<br /> currentBreakpoint = <span class="hljs-string">'tablet'</span>;<br /> }<br /> <br /> <span class="hljs-built_in">document</span>.body.setAttribute(<span class="hljs-string">'data-breakpoint'</span>, currentBreakpoint);<br /> <br /> <span class="hljs-comment">// Dispatch custom event</span><br /> <span class="hljs-built_in">window</span>.dispatchEvent(<span class="hljs-keyword">new</span> CustomEvent(<span class="hljs-string">'breakpointchange'</span>, {<br /> <span class="hljs-attr">detail</span>: { width, <span class="hljs-attr">breakpoint</span>: currentBreakpoint }<br /> }));<br />};<br /><br />watchWindowSize(breakpointHandler);</code></pre>
|
|
63
|
+
|
|
64
|
+
<h4>Performance Benefits</h4>
|
|
65
|
+
<p>The utility automatically debounces resize events, so you don't need to worry about performance:</p>
|
|
66
|
+
<pre><code class="hljs javascript"><span class="hljs-comment">// This handler will only be called after resize events stop</span><br /><span class="hljs-keyword">const</span> expensiveHandler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-comment">// Expensive DOM calculations or API calls</span><br /> recalculateLayout();<br /> updateCharts();<br /> reflow();<br />};<br /><br />watchWindowSize(expensiveHandler); <span class="hljs-comment">// Automatically debounced</span></code></pre>
|
|
146
67
|
|
|
147
|
-
<h3 id="events"><a href="#events" class="no-link">Events</a></h3>
|
|
148
|
-
<h5><code>openpanel</code></h5>
|
|
149
|
-
<p>Fired when a panel is opened. The event detail contains <code>{ panelName }</code>.</p>
|
|
150
|
-
<h5><code>closepanel</code></h5>
|
|
151
|
-
<p>Fired when a panel is closed. The event detail contains <code>{ panelName }</code>.</p>
|
|
152
|
-
<h5><code>togglepanel</code></h5>
|
|
153
|
-
<p>Fired when a panel is toggled. The event detail contains <code>{ panelName }</code>.</p>
|
|
154
|
-
|
|
155
68
|
</main>
|
|
156
69
|
<div style="height:33vh"></div>
|
|
157
|
-
<script type="module" src="../src/components/Accordion.js"></script>
|
|
158
70
|
</body>
|
|
159
|
-
</html>
|
|
71
|
+
</html>
|
|
@@ -3,83 +3,69 @@
|
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
-
<title>
|
|
7
|
-
<link rel="stylesheet" href="../kempo-vars.css"
|
|
6
|
+
<title>watchWindowSize - Kempo UI</title>
|
|
7
|
+
<link rel="stylesheet" href="../kempo-vars.css" />
|
|
8
|
+
<link rel="stylesheet" href="../kempo.min.css" />
|
|
8
9
|
<link rel="stylesheet" href="../kempo-hljs.css" />
|
|
9
10
|
<link rel="stylesheet" href="../styles.css" />
|
|
11
|
+
<script>window.litDisableBundleWarning = true;</script>
|
|
10
12
|
<script type="module" src="./init.js"></script>
|
|
11
13
|
</head>
|
|
12
14
|
<body>
|
|
13
15
|
<k-import src="../nav.inc.html"></k-import>
|
|
14
|
-
<h1 class="ta-center">Card</h1>
|
|
15
16
|
<main>
|
|
16
|
-
<
|
|
17
|
+
<h1>watchWindowSize</h1>
|
|
18
|
+
|
|
19
|
+
<details
|
|
20
|
+
class="b r mb"
|
|
21
|
+
>
|
|
17
22
|
<summary class="p">Table of Contents</summary>
|
|
18
23
|
<div class="m mt0 pl">
|
|
19
|
-
<h6>
|
|
24
|
+
<h6>Usage</h6>
|
|
20
25
|
<a href="#basicUsage">Basic Usage</a><br />
|
|
21
|
-
|
|
22
|
-
<
|
|
23
|
-
<a href="#constructor">Constructor</a><br />
|
|
24
|
-
<a href="#requirements">Requirements</a><br />
|
|
25
|
-
<a href="#properties">Properties</a><br />
|
|
26
|
-
<a href="#methods">Methods</a><br />
|
|
26
|
+
<a href="#functions">Functions</a><br />
|
|
27
|
+
<a href="#examples">Examples</a><br />
|
|
27
28
|
</div>
|
|
28
29
|
</details>
|
|
29
30
|
|
|
30
|
-
<
|
|
31
|
-
<
|
|
32
|
-
<div class="col d-span-6 m-span-12 px">
|
|
33
|
-
<pre><code class="hljs xml"><span class="hljs-tag"><<span class="hljs-name">k-card</span>></span><br /> This is a card<br /><span class="hljs-tag"></<span class="hljs-name">k-card</span>></span></code></pre>
|
|
34
|
-
</div>
|
|
35
|
-
<div class="col d-span-6 m-span-12 px">
|
|
36
|
-
<k-card>
|
|
37
|
-
This is a card
|
|
38
|
-
</k-card>
|
|
39
|
-
</div>
|
|
40
|
-
</div>
|
|
41
|
-
|
|
42
|
-
<h3 id="labels"><a href="#labels" class="no-link">Labels</a></h3>
|
|
43
|
-
<p>A label gives some text in a box on the top border of the card, helping identify the purpose of the contents.</p>
|
|
44
|
-
<div class="row -mx mb">
|
|
45
|
-
<div class="col d-span-6 m-span-12 px">
|
|
46
|
-
<pre><code class="hljs xml"><span class="hljs-tag"><<span class="hljs-name">k-card</span> <span class="hljs-attr">label</span>=<span class="hljs-string">"I'm a Label"</span>></span><br /> This is a card<br /><span class="hljs-tag"></<span class="hljs-name">k-card</span>></span></code></pre>
|
|
47
|
-
</div>
|
|
48
|
-
<div class="col d-span-6 m-span-12 px">
|
|
49
|
-
<k-card label="I'm a Label">
|
|
50
|
-
This is a card
|
|
51
|
-
</k-card>
|
|
52
|
-
</div>
|
|
53
|
-
</div>
|
|
31
|
+
<h3>Description</h3>
|
|
32
|
+
<p>The <code>watchWindowSize</code> utility provides a debounced way to monitor window resize events. It manages multiple handlers efficiently and automatically debounces resize events to improve performance.</p>
|
|
54
33
|
|
|
55
|
-
<
|
|
34
|
+
<h3 id="basicUsage"><a href="#basicUsage" class="no-link">Basic Usage</a></h3>
|
|
35
|
+
<p>Import and use the window size watching functions:</p>
|
|
36
|
+
<pre><code class="hljs javascript"><span class="hljs-keyword">import</span> { watchWindowSize, unwatchWindowSize } <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/watchWindowSize.js'</span>;<br /><br /><span class="hljs-keyword">const</span> handler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Window width:'</span>, width);<br />};<br /><br /><span class="hljs-comment">// Start watching</span><br /><span class="hljs-keyword">const</span> currentWidth = watchWindowSize(handler);<br /><br /><span class="hljs-comment">// Stop watching when done</span><br />unwatchWindowSize(handler);</code></pre>
|
|
56
37
|
|
|
57
|
-
<h3 id="
|
|
58
|
-
|
|
59
|
-
<h5>
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
<h5><code>label<i>: string</i></code></h5>
|
|
66
|
-
<p>The label for the card.</p>
|
|
38
|
+
<h3 id="functions"><a href="#functions" class="no-link">Functions</a></h3>
|
|
39
|
+
|
|
40
|
+
<h5><code>watchWindowSize(handler)</code></h5>
|
|
41
|
+
<p>Registers a handler function to be called when the window is resized.</p>
|
|
42
|
+
<ul>
|
|
43
|
+
<li><strong>handler:</strong> Function that receives the new window width</li>
|
|
44
|
+
<li><strong>Returns:</strong> Current window width</li>
|
|
45
|
+
</ul>
|
|
67
46
|
|
|
68
|
-
<
|
|
47
|
+
<h5><code>unwatchWindowSize(handler)</code></h5>
|
|
48
|
+
<p>Unregisters a previously registered handler function.</p>
|
|
69
49
|
<ul>
|
|
70
|
-
<li><
|
|
50
|
+
<li><strong>handler:</strong> The same function reference that was registered</li>
|
|
71
51
|
</ul>
|
|
72
52
|
|
|
73
|
-
<h3 id="
|
|
74
|
-
|
|
75
|
-
<
|
|
53
|
+
<h3 id="examples"><a href="#examples" class="no-link">Examples</a></h3>
|
|
54
|
+
|
|
55
|
+
<h4>Responsive Component</h4>
|
|
56
|
+
<pre><code class="hljs javascript"><span class="hljs-keyword">import</span> { watchWindowSize, unwatchWindowSize } <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/watchWindowSize.js'</span>;<br /><br /><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ResponsiveComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">LitElement</span> </span>{<br /> <span class="hljs-keyword">constructor</span>() {<br /> <span class="hljs-keyword">super</span>();<br /> <span class="hljs-keyword">this</span>.isMobile = <span class="hljs-literal">false</span>;<br /> <span class="hljs-keyword">this</span>.resizeHandler = <span class="hljs-keyword">this</span>.handleResize.bind(<span class="hljs-keyword">this</span>);<br /> }<br /><br /> connectedCallback() {<br /> <span class="hljs-keyword">super</span>.connectedCallback();<br /> <span class="hljs-keyword">const</span> currentWidth = watchWindowSize(<span class="hljs-keyword">this</span>.resizeHandler);<br /> <span class="hljs-keyword">this</span>.handleResize(currentWidth);<br /> }<br /><br /> disconnectedCallback() {<br /> <span class="hljs-keyword">super</span>.disconnectedCallback();<br /> unwatchWindowSize(<span class="hljs-keyword">this</span>.resizeHandler);<br /> }<br /><br /> handleResize(width) {<br /> <span class="hljs-keyword">this</span>.isMobile = width < <span class="hljs-number">768</span>;<br /> <span class="hljs-keyword">this</span>.requestUpdate();<br /> }<br /><br /> render() {<br /> <span class="hljs-keyword">return</span> html`<span class="xml"><br /> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"</span></span></span><span class="hljs-subst">${<span class="hljs-keyword">this</span>.isMobile ? <span class="hljs-string">'mobile'</span> : <span class="hljs-string">'desktop'</span>}</span><span class="xml"><span class="hljs-tag"><span class="hljs-string">"</span>></span><br /> Content adapts to screen size<br /> <span class="hljs-tag"></<span class="hljs-name">div</span>></span><br /> `</span>;<br /> }<br />}</code></pre>
|
|
57
|
+
|
|
58
|
+
<h4>Multiple Handlers</h4>
|
|
59
|
+
<pre><code class="hljs javascript"><span class="hljs-comment">// Handler for navigation</span><br /><span class="hljs-keyword">const</span> navHandler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-keyword">const</span> nav = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'.navigation'</span>);<br /> nav.classList.toggle(<span class="hljs-string">'mobile'</span>, width < <span class="hljs-number">1024</span>);<br />};<br /><br /><span class="hljs-comment">// Handler for sidebar</span><br /><span class="hljs-keyword">const</span> sidebarHandler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-keyword">const</span> sidebar = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'.sidebar'</span>);<br /> sidebar.style.display = width < <span class="hljs-number">768</span> ? <span class="hljs-string">'none'</span> : <span class="hljs-string">'block'</span>;<br />};<br /><br /><span class="hljs-comment">// Both handlers will be called on resize</span><br />watchWindowSize(navHandler);<br />watchWindowSize(sidebarHandler);<br /><br /><span class="hljs-comment">// Clean up when needed</span><br />unwatchWindowSize(navHandler);<br />unwatchWindowSize(sidebarHandler);</code></pre>
|
|
60
|
+
|
|
61
|
+
<h4>Breakpoint Detection</h4>
|
|
62
|
+
<pre><code class="hljs javascript"><span class="hljs-keyword">const</span> breakpoints = {<br /> <span class="hljs-attr">mobile</span>: <span class="hljs-number">768</span>,<br /> <span class="hljs-attr">tablet</span>: <span class="hljs-number">1024</span>,<br /> <span class="hljs-attr">desktop</span>: <span class="hljs-number">1200</span><br />};<br /><br /><span class="hljs-keyword">const</span> breakpointHandler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-keyword">let</span> currentBreakpoint = <span class="hljs-string">'mobile'</span>;<br /> <br /> <span class="hljs-keyword">if</span> (width >= breakpoints.desktop) {<br /> currentBreakpoint = <span class="hljs-string">'desktop'</span>;<br /> } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (width >= breakpoints.tablet) {<br /> currentBreakpoint = <span class="hljs-string">'tablet'</span>;<br /> }<br /> <br /> <span class="hljs-built_in">document</span>.body.setAttribute(<span class="hljs-string">'data-breakpoint'</span>, currentBreakpoint);<br /> <br /> <span class="hljs-comment">// Dispatch custom event</span><br /> <span class="hljs-built_in">window</span>.dispatchEvent(<span class="hljs-keyword">new</span> CustomEvent(<span class="hljs-string">'breakpointchange'</span>, {<br /> <span class="hljs-attr">detail</span>: { width, <span class="hljs-attr">breakpoint</span>: currentBreakpoint }<br /> }));<br />};<br /><br />watchWindowSize(breakpointHandler);</code></pre>
|
|
76
63
|
|
|
77
|
-
<
|
|
78
|
-
<
|
|
79
|
-
<
|
|
64
|
+
<h4>Performance Benefits</h4>
|
|
65
|
+
<p>The utility automatically debounces resize events, so you don't need to worry about performance:</p>
|
|
66
|
+
<pre><code class="hljs javascript"><span class="hljs-comment">// This handler will only be called after resize events stop</span><br /><span class="hljs-keyword">const</span> expensiveHandler = <span class="hljs-function">(<span class="hljs-params">width</span>) =></span> {<br /> <span class="hljs-comment">// Expensive DOM calculations or API calls</span><br /> recalculateLayout();<br /> updateCharts();<br /> reflow();<br />};<br /><br />watchWindowSize(expensiveHandler); <span class="hljs-comment">// Automatically debounced</span></code></pre>
|
|
80
67
|
|
|
81
68
|
</main>
|
|
82
69
|
<div style="height:33vh"></div>
|
|
83
|
-
<script type="module" src="../src/components/Card.js"></script>
|
|
84
70
|
</body>
|
|
85
|
-
</html>
|
|
71
|
+
</html>
|