|
| 1 | +# Launch Darkly Svelte SDK |
| 2 | + |
| 3 | +This is a Svelte library for Launch Darkly. It is a wrapper around the official Launch Darkly JavaScript SDK but with a Svelte-friendly API. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Getting Started](#getting-started) |
| 8 | +- [Advanced Usage](#advanced-usage) |
| 9 | + - [Changing user context](#changing-user-context) |
| 10 | + - [Getting feature flag values](#getting-feature-flag-values) |
| 11 | + - [Getting immediate flag value](#getting-immediate-flag-value) |
| 12 | + - [Watching flag value changes](#watching-flag-value-changes) |
| 13 | + - [Getting all flag values](#getting-all-flag-values) |
| 14 | + |
| 15 | +## Getting started |
| 16 | + |
| 17 | +First, install the package: |
| 18 | + |
| 19 | +```bash |
| 20 | +npm install @launchdarkly/svelte-client-sdk # or use yarn or pnpm |
| 21 | +``` |
| 22 | + |
| 23 | +Then, initialize the SDK with your client-side ID using the `LDProvider` component: |
| 24 | + |
| 25 | +```svelte |
| 26 | +<script> |
| 27 | + import { LDProvider } from '@launchdarkly/svelte-client-sdk'; |
| 28 | + import App from './App.svelte'; |
| 29 | +</script> |
| 30 | +
|
| 31 | +// Use context relevant to your application |
| 32 | +const context = { |
| 33 | + user: { |
| 34 | + key: 'user-key', |
| 35 | + }, |
| 36 | +}; |
| 37 | +
|
| 38 | +<LDProvider clientID="your-client-side-id" {context}> |
| 39 | + <App /> |
| 40 | +</LDProvider> |
| 41 | +``` |
| 42 | + |
| 43 | +Now you can use the `LDFlag` component to conditionally render content based on feature flags: |
| 44 | + |
| 45 | +```svelte |
| 46 | +<script> |
| 47 | + import { LDFlag } from '@launchdarkly/svelte-client-sdk'; |
| 48 | +</script> |
| 49 | +
|
| 50 | +<LDFlag flag={'my-feature-flag'}> |
| 51 | + <div slot="on"> |
| 52 | + <p>this will render if the feature flag is on</p> |
| 53 | + </div> |
| 54 | + <div slot="off"> |
| 55 | + <p>this will render if the feature flag is off</p> |
| 56 | + </div> |
| 57 | +</LDFlag> |
| 58 | +``` |
| 59 | + |
| 60 | +## Advanced usage |
| 61 | + |
| 62 | +### Changing user context |
| 63 | + |
| 64 | +You can change the user context by using the `identify` function from the `LD` object: |
| 65 | + |
| 66 | +```svelte |
| 67 | +<script> |
| 68 | + import { LD } from '@launchdarkly/svelte-client-sdk'; |
| 69 | +
|
| 70 | + function handleLogin() { |
| 71 | + LD.identify({ key: 'new-user-key' }); |
| 72 | + } |
| 73 | +</script> |
| 74 | +
|
| 75 | +<button on:click={handleLogin}>Login</button> |
| 76 | +``` |
| 77 | + |
| 78 | +### Getting feature flag values |
| 79 | + |
| 80 | +#### Getting immediate flag value |
| 81 | + |
| 82 | +If you need to get the value of a flag at time of evaluation you can use the `useFlag` function: |
| 83 | + |
| 84 | +```svelte |
| 85 | +<script> |
| 86 | + import { LD } from '@launchdarkly/svelte-client-sdk'; |
| 87 | +
|
| 88 | + function handleClick() { |
| 89 | + const isFeatureFlagOn = LD.useFlag('my-feature-flag', false); |
| 90 | + console.log(isFeatureFlagOn); |
| 91 | + } |
| 92 | +</script> |
| 93 | +
|
| 94 | +<button on:click={handleClick}>Check flag value</button> |
| 95 | +``` |
| 96 | + |
| 97 | +**Note:** Please note that `useFlag` function will return the current value of the flag at the time of evaluation, which means you won't get notified if the flag value changes. This is useful for cases where you need to get the value of a flag at a specific time like a function call. If you need to get notified when the flag value changes, you should use the `LDFlag` component, the `watch` function or the `flags` object depending on your use case. |
| 98 | + |
| 99 | +#### Watching flag value changes |
| 100 | + |
| 101 | +If you need to get notified when a flag value changes you can use the `watch` function. The `watch` function is an instance of [Svelte Store](https://svelte.dev/docs/svelte-store), which means you can use it with the `$` store subscriber syntax or the `subscribe` method. Here is an example of how to use the `watch` function: |
| 102 | + |
| 103 | +```svelte |
| 104 | +<script> |
| 105 | + import { LD } from '@launchdarkly/svelte-client-sdk'; |
| 106 | +
|
| 107 | + $: flagValue = LD.watch('my-feature-flag'); |
| 108 | +</script> |
| 109 | +
|
| 110 | +<p>{$flagValue}</p> |
| 111 | +``` |
| 112 | + |
| 113 | +#### Getting all flag values |
| 114 | + |
| 115 | +If you need to get all flag values you can use the `flags` object. The `flags` object is an instance of [Svelte Store](https://svelte.dev/docs/svelte-store), which means you can use it with the `$` store subscriber syntax or the `subscribe` method. Here is an example of how to use the `flags` object: |
| 116 | + |
| 117 | +```svelte |
| 118 | +<script> |
| 119 | + import { LD } from '@launchdarkly/svelte-client-sdk'; |
| 120 | +
|
| 121 | + $: allFlags = LD.flags; |
| 122 | +</script> |
| 123 | +
|
| 124 | +{#each Object.keys($allFlags) as flagName} |
| 125 | + <p>{flagName}: {$allFlags[flagName]}</p> |
| 126 | +{/each} |
| 127 | +``` |
| 128 | + |
| 129 | +## Credits |
| 130 | + |
| 131 | +- Original code by [Robinson Marquez](https://github.com/nosnibor89) |
0 commit comments