Skip to content

Commit

Permalink
[@mantine/hooks] use-local-storage: Fix error throwing if localStorag…
Browse files Browse the repository at this point in the history
…e/sessionStorage is blocked by browser (#5091)
  • Loading branch information
yongwee authored Oct 22, 2023
1 parent 05e26d7 commit a8e1615
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
8 changes: 4 additions & 4 deletions docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@
version "0.0.0"
uid ""

"@mantine/[email protected].3":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@mantine/store/-/store-7.1.3.tgz#43002373de5c0aa0500656e0fc5ea90c3cd8524d"
integrity sha512-fjgZyW9TPk/rCewYaMygyUB6Bos0PspgYiSGCx3g81OzzdTjBhtq+xU2Lgb8YPaPhMsTDWZI+TOqceqQg12Cqw==
"@mantine/[email protected].5":
version "7.1.5"
resolved "https://registry.yarnpkg.com/@mantine/store/-/store-7.1.5.tgz#f77457e0b2c52e24d28b9ce66ad7591272b7bd93"
integrity sha512-iPAt8auWyUs5TyUr31MziCILlLCYCfw6fSqPvLxOwUYpSf9BvtCAoE9JmrRrVi2q5+xO0KPSyK+OHWyBwsAqcQ==

"@mantine/store@link:../src/mantine-store":
version "0.0.0"
Expand Down
19 changes: 13 additions & 6 deletions src/mantine-hooks/src/use-local-storage/create-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,19 @@ export function createStorage<T>(type: StorageType, hookName: string) {
}: StorageProperties<T>) {
const readStorageValue = useCallback(
(skipStorage?: boolean): T => {
if (
typeof window === 'undefined' ||
!(type in window) ||
window[type] === null ||
skipStorage
) {
let storageBlockedOrSkipped;

try {
storageBlockedOrSkipped =
typeof window === 'undefined' ||
!(type in window) ||
window[type] === null ||
!!skipStorage;
} catch (_e) {
storageBlockedOrSkipped = true;
}

if (storageBlockedOrSkipped) {
return defaultValue as T;
}

Expand Down

0 comments on commit a8e1615

Please sign in to comment.