Skip to content

Commit 8f17492

Browse files
committed
Pass live api through context not a prop
1 parent 474304a commit 8f17492

File tree

19 files changed

+37
-26
lines changed

19 files changed

+37
-26
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,12 @@ If you have examples you want to add, feel free to create a PR, I'd be happy to
217217

218218
```svelte
219219
<script>
220+
import {getContext} from "svelte"
220221
// The number prop is reactive,
221222
// this means if the server assigns the number, it will update in the frontend
222223
export let number = 1
223224
// live contains all exported LiveView methods available to the frontend
224-
export let live
225+
const live = getContext("live-svelte")
225226
226227
function increase() {
227228
// This pushes the event over the websocket
@@ -236,7 +237,7 @@ If you have examples you want to add, feel free to create a PR, I'd be happy to
236237
}
237238
238239
function decrease() {
239-
pushEvent("set_number", {number: number - 1}, () => {})
240+
live.pushEvent("set_number", {number: number - 1}, () => {})
240241
}
241242
</script>
242243

assets/js/live_svelte/hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ function getProps(ref) {
7171
return {
7272
...getAttributeJson(ref, "data-props"),
7373
...getLiveJsonProps(ref),
74-
live: ref,
7574
$$slots: getSlots(ref),
7675
$$scope: {},
7776
}
@@ -106,6 +105,7 @@ export function getHooks(components) {
106105
this._instance = new Component({
107106
target: this.el,
108107
props: getProps(this),
108+
context: new Map([['live-svelte', this]]),
109109
hydrate: this.el.hasAttribute("data-ssr"),
110110
})
111111
},

example_project/assets/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_project/assets/svelte/Chat.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<script>
22
import {fly} from "svelte/transition"
33
import {elasticOut} from "svelte/easing"
4-
import {afterUpdate} from "svelte"
4+
import {getContext, afterUpdate} from "svelte"
55
66
export let messages
77
export let name
8-
export let live
8+
const live = getContext("live-svelte")
99
1010
let body = ""
1111
let messagesElement

example_project/assets/svelte/LightControllers.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<script>
2-
export let live
2+
import {getContext} from "svelte"
3+
34
export let isOn = false
5+
const live = getContext("live-svelte")
6+
47
const toggleLight = () => {
58
isOn = !isOn
69
live.pushEvent(isOn ? "on" : "off")

example_project/assets/svelte/LogList.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<script>
22
import {slide, fly} from "svelte/transition"
3+
import {getContext} from "svelte"
34
4-
export let live
55
export let items = []
6+
const live = getContext("live-svelte")
7+
68
let body
79
let i = 1
810
let showItems = true

example_project/lib/example_web/live/live_breaking_news.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ defmodule ExampleWeb.LiveExample5 do
2121
<script>
2222
import {slide, fly} from "svelte/transition"
2323
import {Marquee} from "dynamic-marquee"
24-
import {onMount} from "svelte"
24+
import {getContext, onMount} from "svelte"
2525
2626
export let news = []
27-
export let live
27+
const live = getContext("live-svelte")
2828
2929
let newItem = ""
3030
let marquee

examples/advanced_chat/AdvancedChat.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<script>
22
import {slide, fly, fade} from "svelte/transition"
33
import {elasticOut} from "svelte/easing"
4-
import {afterUpdate} from "svelte"
4+
import {getContext, afterUpdate} from "svelte"
55
66
export let messages
77
export let name
8-
export let live
8+
const live = getContext("live-svelte")
99
1010
let body = ""
1111
let messagesElement

examples/animated_number/Numbers.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script>
22
import {fly} from "svelte/transition"
3+
import {getContext} from "svelte"
34
45
export let number = 1
5-
export let live
6+
const live = getContext("live-svelte")
67
78
function increase() {
89
live.pushEvent("set_number", {number: number + 1})

examples/breaking_news/BreakingNews.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// https://github.com/tjenkinson/dynamic-marquee
33
import {slide, fly} from "svelte/transition"
44
import {Marquee} from "dynamic-marquee"
5-
import {onMount} from "svelte"
5+
import {getContext, onMount} from "svelte"
66
77
export let news = []
8-
export let live
8+
const live = getContext("live-svelte")
99
1010
let newItem = ""
1111
let marquee

0 commit comments

Comments
 (0)