Skip to content

Commit db587d8

Browse files
author
wusaby-rush
committed
Add $root refer to component root element
Some times like in dialogs we need access to root component from child nodes, also it is a way to access root element from `v-scope`. the next snippet from another PR to expose `$el` to scope, but this PR can solve the same problem also ```html <textarea v-scope="{width: $root.offsetWidth, height: $root.offsetHeight}" @click="width = $el.offsetWidth; height = $el.offsetHeight;" > {{ width }} &times; {{ height }} </textarea> ```
1 parent 2525578 commit db587d8

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
{{ count }}
2828
<button @click="count++">inc</button>
2929
</div>
30+
31+
<!-- another example -->
32+
<textarea
33+
v-scope="{width: $root.offsetWidth, height: $root.offsetHeight}"
34+
@click="width = $el.offsetWidth; height = $el.offsetHeight;"
35+
>
36+
{{ width }} &times; {{ height }}
37+
</textarea>
3038
```
3139

3240
- Use `v-scope` to mark regions on the page that should be controlled by `petite-vue`.
@@ -346,10 +354,11 @@ Check out the [examples directory](https://github.com/vuejs/petite-vue/tree/main
346354
- `v-scope`
347355
- `v-effect`
348356
- `@vue:mounted` & `@vue:unmounted` events
357+
- `$root` refer to component root element
349358

350359
### Has Different Behavior
351360

352-
- In expressions, `$el` points to the current element the directive is bound to (instead of component root element)
361+
- In expressions, `$el` points to the current element the directive is bound to (instead of component root element which accessed by `$root`)
353362
- `createApp()` accepts global state instead of a component
354363
- Components are simplified into object-returning functions
355364
- Custom directives have a different interface

src/walk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const walk = (node: Node, ctx: Context): ChildNode | null | void => {
4040
// v-scope
4141
if ((exp = checkAttr(el, 'v-scope')) || exp === '') {
4242
const scope = exp ? evaluate(ctx.scope, exp) : {}
43+
scope.$root = el
4344
ctx = createScopedContext(ctx, scope)
4445
if (scope.$template) {
4546
resolveTemplate(el, scope.$template)

tests/ref.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
v-scope="{ dynamicRef: 'x', show: true }"
1010
v-effect="console.log({ x: $refs.x, y: $refs.y, input: $refs.input })"
1111
>
12-
<p>Accessing root el: id is {{ $refs.root.id }}</p>
12+
<p>Accessing root el (with ref): id is {{ $refs.root.id }}</p>
13+
<p>Accessing root el (with $root): id is {{ $refs.root.id }}</p>
1314

1415
<input ref="input" />
1516
<span v-if="show" :ref="dynamicRef">Span with dynamic ref</span>

0 commit comments

Comments
 (0)