|
| 1 | +import type { Alpine } from 'alpinejs'; |
| 2 | +import type { Assertion } from 'vitest'; |
| 3 | + |
| 4 | +export const $scope = Symbol('$scope'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Alpine Scope Plugin registers `$scope` magic property and `x-scope` directive. |
| 8 | + * Allows reaching into specific parent component contexts to access their data. |
| 9 | + * @param Alpine {Alpine} |
| 10 | + */ |
| 11 | +export const Scope = (Alpine: Alpine) => { |
| 12 | + type Scopable = { [$scope]?: Map<string, HTMLElement> }; |
| 13 | + Alpine.directive('scope', (el, { expression }) => { |
| 14 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 15 | + const context = Alpine.$data(el) as Scopable; |
| 16 | + |
| 17 | + const rootContext = Alpine.closestDataStack(el)[0]; |
| 18 | + if (!rootContext) return; |
| 19 | + rootContext[$scope] = new Map(context[$scope]).set(expression, el); |
| 20 | + }); |
| 21 | + Alpine.magic('scope', (el) => { |
| 22 | + return new Proxy( |
| 23 | + {}, |
| 24 | + { |
| 25 | + get(_, name: string) { |
| 26 | + const scopes = (Alpine.$data(el) as Scopable)[$scope]; |
| 27 | + if (scopes?.has(name)) return Alpine.$data(scopes.get(name)!); |
| 28 | + const root = Alpine.findClosest(el, (el) => |
| 29 | + el.matches(`[x-data="${name}"]`), |
| 30 | + ) as HTMLElement | undefined; |
| 31 | + if (root) return Alpine.$data(root); |
| 32 | + return undefined; |
| 33 | + }, |
| 34 | + }, |
| 35 | + ); |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +export default Scope; |
| 40 | + |
| 41 | +declare module 'alpinejs' { |
| 42 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 43 | + export interface Magics<T> { |
| 44 | + /** |
| 45 | + * A `Proxy` of the parent component scopes |
| 46 | + */ |
| 47 | + $scope: Record<string, unknown>; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +if (import.meta.vitest) { |
| 52 | + describe('$scope', () => { |
| 53 | + it('can access implicitely scoped context', async () => { |
| 54 | + const root = await render( |
| 55 | + ` |
| 56 | + <div x-data="foo"> |
| 57 | + <div x-data="bar"> |
| 58 | + <span id="naked" x-text="value"></span> |
| 59 | + <span id="foo" x-text="$scope.foo?.value"></span> |
| 60 | + <span id="bar" x-text="$scope.bar?.value"></span> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + `.trim(), |
| 64 | + ) |
| 65 | + .withComponent('foo', () => ({ value: 'foo' })) |
| 66 | + .withComponent('bar', () => ({ value: 'bar' })) |
| 67 | + .withPlugin(Scope); |
| 68 | + expect((root as HTMLElement).querySelector('#naked')).toHaveTextContent( |
| 69 | + 'bar', |
| 70 | + ); |
| 71 | + expect((root as HTMLElement).querySelector('#foo')).toHaveTextContent( |
| 72 | + 'foo', |
| 73 | + ); |
| 74 | + expect((root as HTMLElement).querySelector('#bar')).toHaveTextContent( |
| 75 | + 'bar', |
| 76 | + ); |
| 77 | + }); |
| 78 | + it('can access explicitely scoped context', async () => { |
| 79 | + const root = await render( |
| 80 | + ` |
| 81 | + <div x-data="{ value: 'foo' }" x-scope="foo"> |
| 82 | + <div x-data="{ value: 'bar' }" x-scope="bar"> |
| 83 | + <span id="naked" x-text="value"></span> |
| 84 | + <span id="foo" x-text="$scope.foo?.value"></span> |
| 85 | + <span id="bar" x-text="$scope.bar?.value"></span> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + `.trim(), |
| 89 | + ).withPlugin(Scope); |
| 90 | + expect((root as HTMLElement).querySelector('#naked')).toHaveTextContent( |
| 91 | + 'bar', |
| 92 | + ); |
| 93 | + expect((root as HTMLElement).querySelector('#foo')).toHaveTextContent( |
| 94 | + 'foo', |
| 95 | + ); |
| 96 | + expect((root as HTMLElement).querySelector('#bar')).toHaveTextContent( |
| 97 | + 'bar', |
| 98 | + ); |
| 99 | + }); |
| 100 | + it('favors explicitely scoped contexts', async () => { |
| 101 | + const root = await render( |
| 102 | + ` |
| 103 | + <div x-data="foo" x-scope="bar"> |
| 104 | + <div x-data="bar"> |
| 105 | + <span id="naked" x-text="value"></span> |
| 106 | + <span id="bar" x-text="$scope.bar?.value"></span> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + `.trim(), |
| 110 | + ) |
| 111 | + .withComponent('foo', () => ({ value: 'foo' })) |
| 112 | + .withComponent('bar', () => ({ value: 'bar' })) |
| 113 | + .withPlugin(Scope); |
| 114 | + expect((root as HTMLElement).querySelector('#naked')).toHaveTextContent( |
| 115 | + 'bar', |
| 116 | + ); |
| 117 | + expect((root as HTMLElement).querySelector('#bar')).toHaveTextContent( |
| 118 | + 'foo', |
| 119 | + ); |
| 120 | + }); |
| 121 | + it('does not leak', async () => { |
| 122 | + const root = await render( |
| 123 | + ` |
| 124 | + <div x-data="{ value: 'root' }"> |
| 125 | + <div x-data="foo" x-scope="foo"></div> |
| 126 | + <div x-data="bar"> |
| 127 | + </div> |
| 128 | + <span id="naked" x-text="value"></span> |
| 129 | + <span id="foo" x-text="$scope.foo?.value ?? 'not found'"></span> |
| 130 | + <span id="bar" x-text="$scope.bar?.value ?? 'not found'"></span> |
| 131 | + </div> |
| 132 | + `.trim(), |
| 133 | + ) |
| 134 | + .withComponent('foo', () => ({ value: 'foo' })) |
| 135 | + .withComponent('bar', () => ({ value: 'bar' })) |
| 136 | + .withPlugin(Scope); |
| 137 | + expect((root as HTMLElement).querySelector('#naked')).toHaveTextContent( |
| 138 | + 'root', |
| 139 | + ); |
| 140 | + expect((root as HTMLElement).querySelector('#foo')).toHaveTextContent( |
| 141 | + 'not found', |
| 142 | + ); |
| 143 | + expect((root as HTMLElement).querySelector('#bar')).toHaveTextContent( |
| 144 | + 'not found', |
| 145 | + ); |
| 146 | + }); |
| 147 | + }); |
| 148 | +} |
| 149 | +declare module 'vitest' { |
| 150 | + interface Assertion<T> extends AlpineMatchers<T> {} |
| 151 | +} |
| 152 | + |
| 153 | +interface AlpineMatchers<T> { |
| 154 | + toHaveTextContent: (expected: string) => Assertion<T>; |
| 155 | +} |
0 commit comments