-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(modal): trapInEl moved to app context (#4165)
* fix(modal): trapInEl moved to app context * chore(modal): improve focusTrap story * fix(modal): make app global reactive * chore: document useAppGlobal
- Loading branch information
Showing
4 changed files
with
105 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { WritableComputedRef, computed, getCurrentInstance, reactive } from 'vue' | ||
|
||
const getGlobalObject = () => { | ||
const vm = getCurrentInstance() | ||
|
||
const app = vm?.appContext.app | ||
|
||
const { globalProperties } = app!.config | ||
|
||
if ('$vaGlobalVariable' in globalProperties) { | ||
return globalProperties.$vaGlobalVariable | ||
} | ||
|
||
globalProperties.$vaGlobalVariable = reactive({}) | ||
|
||
return globalProperties.$vaGlobalVariable | ||
} | ||
|
||
/** | ||
* This composable must be used to make global variables. This global is shared in app context, rather then | ||
* in window context. This is useful to avoid global variables in window context in multiple app mode, ssr | ||
* or cjs build can mess up global variables | ||
*/ | ||
export const useAppGlobal = <T>(key: string, defaultValue: T): WritableComputedRef<T> => { | ||
const globalObject = getGlobalObject() | ||
|
||
if (key in globalObject) { | ||
return globalObject[key] | ||
} | ||
|
||
globalObject[key] = defaultValue | ||
|
||
return computed({ | ||
get: () => globalObject[key], | ||
set: (value: T) => { | ||
globalObject[key] = value | ||
}, | ||
}) | ||
} |
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