11// Make the <aside> sidebar resizable via a draggable handle. The chosen
2- // width is persisted to localStorage so it survives reloads.
2+ // width is persisted to localStorage so it survives reloads.
33
4- const STORAGE_KEY = 'mws-sidebar-width' ;
5- const MIN_WIDTH = 220 ;
6- const MAX_WIDTH = 900 ;
4+ const STORAGE_KEY = 'mws-sidebar-width' ;
5+ const MIN_WIDTH = 220 ;
6+ const MAX_WIDTH = 900 ;
77
8- /**
9- * Wire a draggable resize handle onto the sidebar.
10- * @param {Document|HTMLElement } root
11- */
12- export function wireSidebarResize ( root = document ) {
13- const aside = root . querySelector ( 'aside' ) ;
14- if ( ! aside ) return ;
8+ /**
9+ * Wire a draggable resize handle onto the sidebar.
10+ * @param {Document|HTMLElement } root
11+ */
12+ export function wireSidebarResize ( root = document ) {
13+ const aside = root . querySelector ( 'aside' ) ;
14+ if ( ! aside ) return ;
1515
16- // Restore any persisted width.
17- try {
18- const saved = parseFloat ( localStorage . getItem ( STORAGE_KEY ) ) ;
19- if ( Number . isFinite ( saved ) && saved >= MIN_WIDTH ) {
20- aside . style . width = `${ saved } px` ;
21- }
22- } catch {
23- /* ignore storage errors */
24- }
16+ // Restore any persisted width.
17+ try {
18+ const saved = parseFloat ( localStorage . getItem ( STORAGE_KEY ) ) ;
19+ if ( Number . isFinite ( saved ) && saved >= MIN_WIDTH ) {
20+ aside . style . width = `${ saved } px` ;
21+ }
22+ } catch {
23+ /* ignore storage errors */
24+ }
2525
26- // Create the handle if it doesn't already exist.
27- let handle = aside . querySelector ( '.sidebar-resize-handle' ) ;
28- if ( ! handle ) {
29- handle = document . createElement ( 'div' ) ;
30- handle . className = 'sidebar-resize-handle' ;
31- handle . title = 'Drag to resize' ;
32- aside . appendChild ( handle ) ;
33- }
26+ // Create the handle if it doesn't already exist.
27+ let handle = aside . querySelector ( '.sidebar-resize-handle' ) ;
28+ if ( ! handle ) {
29+ handle = document . createElement ( 'div' ) ;
30+ handle . className = 'sidebar-resize-handle' ;
31+ handle . title = 'Drag to resize' ;
32+ aside . appendChild ( handle ) ;
33+ }
3434
35- let startX = 0 ;
36- let startWidth = 0 ;
35+ let startX = 0 ;
36+ let startWidth = 0 ;
3737
38- const onMove = ( e ) => {
39- const clientX = e . touches ? e . touches [ 0 ] . clientX : e . clientX ;
40- let next = startWidth + ( clientX - startX ) ;
41- next = Math . max ( MIN_WIDTH , Math . min ( MAX_WIDTH , next ) ) ;
42- aside . style . width = `${ next } px` ;
43- } ;
38+ const onMove = ( e ) => {
39+ const clientX = e . touches ? e . touches [ 0 ] . clientX : e . clientX ;
40+ let next = startWidth + ( clientX - startX ) ;
41+ next = Math . max ( MIN_WIDTH , Math . min ( MAX_WIDTH , next ) ) ;
42+ aside . style . width = `${ next } px` ;
43+ } ;
4444
45- const onUp = ( ) => {
46- document . removeEventListener ( 'mousemove' , onMove ) ;
47- document . removeEventListener ( 'mouseup' , onUp ) ;
48- document . removeEventListener ( 'touchmove' , onMove ) ;
49- document . removeEventListener ( 'touchend' , onUp ) ;
50- document . body . classList . remove ( 'resizing-sidebar' ) ;
51- try {
52- localStorage . setItem ( STORAGE_KEY , String ( parseFloat ( aside . style . width ) || MIN_WIDTH ) ) ;
53- } catch {
54- /* ignore */
55- }
56- // Notify listeners (e.g. grid re-fit) that layout changed.
57- window . dispatchEvent ( new Event ( 'resize' ) ) ;
58- } ;
45+ const onUp = ( ) => {
46+ document . removeEventListener ( 'mousemove' , onMove ) ;
47+ document . removeEventListener ( 'mouseup' , onUp ) ;
48+ document . removeEventListener ( 'touchmove' , onMove ) ;
49+ document . removeEventListener ( 'touchend' , onUp ) ;
50+ document . body . classList . remove ( 'resizing-sidebar' ) ;
51+ try {
52+ localStorage . setItem ( STORAGE_KEY , String ( parseFloat ( aside . style . width ) || MIN_WIDTH ) ) ;
53+ } catch {
54+ /* ignore */
55+ }
56+ // Notify listeners (e.g. grid re-fit) that layout changed.
57+ window . dispatchEvent ( new Event ( 'resize' ) ) ;
58+ } ;
5959
60- const onDown = ( e ) => {
61- e . preventDefault ( ) ;
62- startX = e . touches ? e . touches [ 0 ] . clientX : e . clientX ;
63- startWidth = aside . getBoundingClientRect ( ) . width ;
64- document . body . classList . add ( 'resizing-sidebar' ) ;
65- document . addEventListener ( 'mousemove' , onMove ) ;
66- document . addEventListener ( 'mouseup' , onUp ) ;
67- document . addEventListener ( 'touchmove' , onMove , { passive : false } ) ;
68- document . addEventListener ( 'touchend' , onUp ) ;
69- } ;
60+ const onDown = ( e ) => {
61+ e . preventDefault ( ) ;
62+ startX = e . touches ? e . touches [ 0 ] . clientX : e . clientX ;
63+ startWidth = aside . getBoundingClientRect ( ) . width ;
64+ document . body . classList . add ( 'resizing-sidebar' ) ;
65+ document . addEventListener ( 'mousemove' , onMove ) ;
66+ document . addEventListener ( 'mouseup' , onUp ) ;
67+ document . addEventListener ( 'touchmove' , onMove , { passive : false } ) ;
68+ document . addEventListener ( 'touchend' , onUp ) ;
69+ } ;
7070
71- handle . addEventListener ( 'mousedown' , onDown ) ;
72- handle . addEventListener ( 'touchstart' , onDown , { passive : false } ) ;
73- }
71+ handle . addEventListener ( 'mousedown' , onDown ) ;
72+ handle . addEventListener ( 'touchstart' , onDown , { passive : false } ) ;
73+ }
0 commit comments