-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
84 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"runed": minor | ||
--- | ||
|
||
feat: `useSupported` |
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
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 @@ | ||
export * from "./useSupported.svelte.js"; |
25 changes: 25 additions & 0 deletions
25
packages/runed/src/lib/functions/useSupported/useSupported.svelte.ts
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,25 @@ | ||
import { type ReadableBox, box } from "../box/box.svelte.js"; | ||
|
||
/** | ||
* A hook that takes a predicate determine if a browser API is supported. | ||
* | ||
* Useful for checking if a browser API is supported before attempting to use it. | ||
* | ||
* @example | ||
* ```ts | ||
* const isSupported = useSupported(() => navigator); | ||
* | ||
* if (isSupported) { | ||
* // do something with navigator | ||
* } | ||
* ``` | ||
*/ | ||
export function useSupported(predicate: () => unknown): ReadableBox<boolean> { | ||
const isSupported = box(false); | ||
|
||
$effect(() => { | ||
isSupported.value = Boolean(predicate()); | ||
}); | ||
|
||
return box.readonly(isSupported); | ||
} |
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,18 @@ | ||
--- | ||
title: useSupported | ||
description: Determine if a feature is supported by the environment before using it. | ||
--- | ||
|
||
## Usage | ||
|
||
```svelte | ||
<script lang="ts"> | ||
import { useSupported } from "runed"; | ||
const isSupported = useSupported(() => navigator && "geolocation" in navigator); | ||
if (isSupported.value) { | ||
// Do something with the geolocation API | ||
} | ||
</script> | ||
``` |
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