Close all modals on route change
This commit is contained in:
		
							parent
							
								
									443c30b7ab
								
							
						
					
					
						commit
						db6e5898df
					
				| 
						 | 
					@ -9,7 +9,7 @@
 | 
				
			||||||
  import NewChat from './lib/NewChat.svelte'
 | 
					  import NewChat from './lib/NewChat.svelte'
 | 
				
			||||||
  import { chatsStorage, apiKeyStorage } from './lib/Storage.svelte'
 | 
					  import { chatsStorage, apiKeyStorage } from './lib/Storage.svelte'
 | 
				
			||||||
  import { Modals, closeModal } from 'svelte-modals'
 | 
					  import { Modals, closeModal } from 'svelte-modals'
 | 
				
			||||||
  import { triggerModalEsc } from './lib/Util.svelte'
 | 
					  import { dispatchModalEsc, checkModalEsc } from './lib/Util.svelte'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // The definition of the routes with some conditions
 | 
					  // The definition of the routes with some conditions
 | 
				
			||||||
  const routes = {
 | 
					  const routes = {
 | 
				
			||||||
| 
						 | 
					@ -31,6 +31,14 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    '*': Home
 | 
					    '*': Home
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const onLocationChange = (...args:any) => {
 | 
				
			||||||
 | 
					    // close all modals on route change
 | 
				
			||||||
 | 
					    dispatchModalEsc()
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  $: onLocationChange($location)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<Navbar />
 | 
					<Navbar />
 | 
				
			||||||
| 
						 | 
					@ -53,7 +61,7 @@
 | 
				
			||||||
</Modals>
 | 
					</Modals>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<svelte:window
 | 
					<svelte:window
 | 
				
			||||||
  on:keydown={(e) => triggerModalEsc(e)}
 | 
					  on:keydown={(e) => checkModalEsc(e)}
 | 
				
			||||||
/>
 | 
					/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<style>
 | 
					<style>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -29,8 +29,12 @@
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  export const triggerModalEsc = (event:KeyboardEvent|undefined):boolean|void => {
 | 
					  export const checkModalEsc = (event:KeyboardEvent|undefined):boolean|void => {
 | 
				
			||||||
    if (!event || event.key !== 'Escape') return
 | 
					    if (!event || event.key !== 'Escape') return
 | 
				
			||||||
 | 
					    dispatchModalEsc()
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  export const dispatchModalEsc = ():boolean|void => {
 | 
				
			||||||
    const stack = Array.from(document.querySelectorAll('.modal')).filter(s =>
 | 
					    const stack = Array.from(document.querySelectorAll('.modal')).filter(s =>
 | 
				
			||||||
      window.getComputedStyle(s).getPropertyValue('display') !== 'none'
 | 
					      window.getComputedStyle(s).getPropertyValue('display') !== 'none'
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue