-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added ui state for dialog and modal management
- Loading branch information
1 parent
9d6bc8e
commit 79d273c
Showing
10 changed files
with
272 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
export interface DrawerProps { | ||
/** | ||
* Controls the open state of the drawer. | ||
*/ | ||
open?: boolean; | ||
|
||
/** | ||
* If the drawer can be dismissed by clicking outside of it. | ||
*/ | ||
dismissable?: boolean; | ||
|
||
onClose?: () => void; | ||
|
||
onOpen?: () => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* A simple hook to get the current pathname | ||
*/ | ||
export function usePathname(override?: string): string { | ||
const [ pathname, setPathname ] = useState<string>(override ?? window.location.pathname); | ||
|
||
useEffect(() => { | ||
if(override) { | ||
setPathname(override); | ||
return; | ||
} | ||
|
||
const handleLocationChange = () => { | ||
setPathname(window.location.pathname); | ||
}; | ||
|
||
// Function to wrap history methods to dispatch events | ||
const wrapHistoryMethod = (method: 'pushState' | 'replaceState') => { | ||
const originalMethod = history[method]; | ||
|
||
return function (this: History, ...args: never[]) { | ||
const result = originalMethod.apply(this, args as unknown as Parameters<typeof originalMethod>); | ||
window.dispatchEvent(new Event('locationchange')); | ||
return result; | ||
}; | ||
}; | ||
|
||
// Wrap the pushState and replaceState methods | ||
history.pushState = wrapHistoryMethod('pushState'); | ||
history.replaceState = wrapHistoryMethod('replaceState'); | ||
|
||
// Listen for popstate and custom locationchange events | ||
window.addEventListener('popstate', handleLocationChange); | ||
window.addEventListener('locationchange', handleLocationChange); | ||
|
||
// Clean up the event listeners on component unmount | ||
return () => { | ||
window.removeEventListener('popstate', handleLocationChange); | ||
window.removeEventListener('locationchange', handleLocationChange); | ||
}; | ||
}, [ override ]); | ||
|
||
return pathname; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { reducer as drawerReducer } from './reducers/drawer.reducer'; | ||
import type { DrawerState } from './reducers/drawer.reducer'; | ||
import type { DrawerAction } from './reducers/drawer.actions'; | ||
|
||
export interface State { | ||
drawer: DrawerState; | ||
} | ||
|
||
export type Action = DrawerAction; | ||
|
||
export const initialState: State = { | ||
drawer: drawerReducer() | ||
}; | ||
|
||
export function reducer(state: State = initialState, action: unknown = {}) { | ||
return { | ||
drawer: drawerReducer(state.drawer, action as DrawerAction), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
export type DrawerAction = { | ||
type: 'drawer/register', | ||
payload: { | ||
id: string; | ||
} | ||
} | { | ||
type: 'drawer/unregister', | ||
payload: { | ||
id: string; | ||
} | ||
} | { | ||
type: 'drawer/open', | ||
payload: { | ||
id: string; | ||
} | ||
} | { | ||
type: 'drawer/close', | ||
payload: { | ||
id: string; | ||
} | ||
}; | ||
|
||
/** | ||
* Registers a drawer. | ||
*/ | ||
export function register(id: string): DrawerAction { | ||
return { | ||
type: 'drawer/register', | ||
payload: { | ||
id, | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Unregisters a drawer. | ||
*/ | ||
export function unregister(id: string): DrawerAction { | ||
return { | ||
type: 'drawer/unregister', | ||
payload: { | ||
id, | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Opens a drawer. | ||
*/ | ||
export function open(id: string): DrawerAction { | ||
return { | ||
type: 'drawer/open', | ||
payload: { | ||
id, | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Closes a drawer. | ||
*/ | ||
export function close(id: string): DrawerAction { | ||
return { | ||
type: 'drawer/close', | ||
payload: { | ||
id, | ||
} | ||
}; | ||
} |
Oops, something went wrong.