├── .gitignore ├── .github └── FUNDING.yml ├── .eslintignore ├── jest.config.js ├── tests └── parent.test.js ├── dist ├── cdn.min.js ├── module.esm.js ├── cdn.js └── module.cjs.js ├── src └── index.js ├── .editorconfig ├── .eslintrc.json ├── LICENSE.md ├── package.json ├── examples └── index.html ├── README.md └── scripts └── build.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .vscode 3 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [ryangjchandler] 2 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | dist/* 2 | node_modules/* 3 | scripts/* 4 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | transform: {}, 3 | } 4 | -------------------------------------------------------------------------------- /tests/parent.test.js: -------------------------------------------------------------------------------- 1 | test('true', () => { 2 | expect(true).toBeTruthy() 3 | }) 4 | -------------------------------------------------------------------------------- /dist/cdn.min.js: -------------------------------------------------------------------------------- 1 | (()=>{function i(t){t.magic("parent",(n,{Alpine:e})=>e.mergeProxies(e.closestDataStack(n).slice(1)))}document.addEventListener("alpine:initializing",()=>{i(window.Alpine)});})(); 2 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export default function (Alpine) { 2 | Alpine.magic('parent', (el, { Alpine }) => { 3 | return Alpine.mergeProxies( 4 | Alpine.closestDataStack(el).slice(1), 5 | ) 6 | }) 7 | } 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | indent_size = 4 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /dist/module.esm.js: -------------------------------------------------------------------------------- 1 | // src/index.js 2 | function src_default(Alpine) { 3 | Alpine.magic("parent", (el, {Alpine: Alpine2}) => { 4 | return Alpine2.mergeProxies(Alpine2.closestDataStack(el).slice(1)); 5 | }); 6 | } 7 | 8 | // builds/module.js 9 | var module_default = src_default; 10 | export { 11 | module_default as default 12 | }; 13 | -------------------------------------------------------------------------------- /dist/cdn.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | // src/index.js 3 | function src_default(Alpine) { 4 | Alpine.magic("parent", (el, {Alpine: Alpine2}) => { 5 | return Alpine2.mergeProxies(Alpine2.closestDataStack(el).slice(1)); 6 | }); 7 | } 8 | 9 | // builds/cdn.js 10 | document.addEventListener("alpine:initializing", () => { 11 | src_default(window.Alpine); 12 | }); 13 | })(); 14 | -------------------------------------------------------------------------------- /dist/module.cjs.js: -------------------------------------------------------------------------------- 1 | var __defProp = Object.defineProperty; 2 | var __markAsModule = (target) => __defProp(target, "__esModule", {value: true}); 3 | var __export = (target, all) => { 4 | for (var name in all) 5 | __defProp(target, name, {get: all[name], enumerable: true}); 6 | }; 7 | 8 | // builds/module.js 9 | __markAsModule(exports); 10 | __export(exports, { 11 | default: () => module_default 12 | }); 13 | 14 | // src/index.js 15 | function src_default(Alpine) { 16 | Alpine.magic("parent", (el, {Alpine: Alpine2}) => { 17 | return Alpine2.mergeProxies(Alpine2.closestDataStack(el).slice(1)); 18 | }); 19 | } 20 | 21 | // builds/module.js 22 | var module_default = src_default; 23 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es2021": true, 5 | "jest": true 6 | }, 7 | "extends": ["eslint:recommended", "standard"], 8 | "parserOptions": { 9 | "ecmaVersion": 12, 10 | "sourceType": "module" 11 | }, 12 | "rules": { 13 | "indent": ["error", 4], 14 | "comma-dangle": ["error", "always-multiline"], 15 | "space-before-function-paren": [ 16 | "error", 17 | { 18 | "anonymous": "always", 19 | "named": "never", 20 | "asyncArrow": "always" 21 | } 22 | ] 23 | }, 24 | "globals": { 25 | "Alpine": "readonly" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | Copyright (c) 2021 Ryan Chandler and contributors 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@ryangjchandler/alpine-parent", 3 | "description": "Adds a handy $parent magic property to your Alpine components.", 4 | "version": "1.0.0", 5 | "author": { 6 | "name": "Ryan Chandler", 7 | "email": "support@ryangjchandler.co.uk", 8 | "url": "https://ryangjchandler.co.uk" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/ryangjchandler/alpine-parent" 13 | }, 14 | "main": "dist/module.cjs.js", 15 | "module": "dist/module.esm.js", 16 | "type": "module", 17 | "devDependencies": { 18 | "@types/jest": "^27.0.2", 19 | "brotli-size": "^4.0.0", 20 | "esbuild": "^0.8.39", 21 | "eslint": "^7.22.0", 22 | "eslint-config-standard": "^16.0.2", 23 | "eslint-plugin-import": "^2.22.1", 24 | "eslint-plugin-node": "^11.1.0", 25 | "eslint-plugin-promise": "^4.3.1", 26 | "eslint-plugin-standard": "^4.1.0", 27 | "jest": "^27.2.4", 28 | "lint-staged": "^10.5.4" 29 | }, 30 | "scripts": { 31 | "test": "npm run build && node --experimental-vm-modules node_modules/.bin/jest", 32 | "build": "node ./scripts/build.js", 33 | "watch": "node ./scripts/build.js --watch", 34 | "lint": "eslint .", 35 | "lint-fix": "eslint . --fix" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /examples/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | $parent 9 | 10 | 11 |
12 |

My value is

13 | 14 |
15 |

My value is

16 |

My parent's value is

17 | 18 |
19 |

My value is

20 |

My parent's value is

21 |

My parent's parent value is

22 | 23 | 24 |
25 |
26 |
27 | 28 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > ✨ Help support the maintenance of this package by [sponsoring me](https://github.com/sponsors/ryangjchandler). 2 | 3 | # Alpine `$parent` 4 | 5 | Access parent components using a handy `$parent` magic variable. 6 | 7 | ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/ryangjchandler/alpine-parent?label=version&style=flat-square) 8 | ![Build size Brotli](https://img.badgesize.io/ryangjchandler/alpine-parent/main/dist/cdn.min.js.svg?compression=gzip&style=flat-square&color=green) 9 | [![Monthly downloads via CDN](https://data.jsdelivr.com/v1/package/npm/@ryangjchandler/alpine-parent/badge)](https://www.jsdelivr.com/package/npm/@ryangjchandler/alpine-parent) 10 | 11 | > This package only supports Alpine v3.x. 12 | 13 | ## About 14 | 15 | This plugin provides a new `$parent` magic property that allows you to interact with a parent component's data object directly. This is useful when you have nested components and conflicting property names but would still like to access the parent properties / methods directly. 16 | 17 | ## Installation 18 | 19 | ### CDN 20 | 21 | Include the following ` 28 | ``` 29 | 30 | ### NPM 31 | 32 | ```bash 33 | npm install @ryangjchandler/alpine-parent 34 | ``` 35 | 36 | Add the `$parent` directive to your project by registering the plugin with Alpine. 37 | 38 | ```js 39 | import Alpine from "alpinejs"; 40 | import Parent from "@ryangjchandler/alpine-parent"; 41 | 42 | Alpine.plugin(Parent); 43 | 44 | window.Alpine = Alpine; 45 | window.Alpine.start(); 46 | ``` 47 | 48 | ## Usage 49 | 50 | Access the `$parent` property in your component: 51 | 52 | ```html 53 |
54 |
55 | My value is and my parent's value is 56 |
57 |
58 | ``` 59 | 60 | The `$parent` property returns a `Proxy`, so any updates to the properties should be reactive. This means you'll be able to use it inside of `x-model`, etc. 61 | 62 | ## Versioning 63 | 64 | This projects follow the [Semantic Versioning](https://semver.org/) guidelines. 65 | 66 | ## License 67 | 68 | Copyright (c) 2021 Ryan Chandler and contributors 69 | 70 | Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details. 71 | -------------------------------------------------------------------------------- /scripts/build.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import brotliSize from 'brotli-size' 3 | import esbuild from 'esbuild' 4 | 5 | (() => { 6 | if (! fs.existsSync(`./dist`)) { 7 | fs.mkdirSync(`./dist`, 0o744); 8 | } 9 | 10 | // Go through each file in the package's "build" directory 11 | // and use the appropriate bundling strategy based on its name. 12 | fs.readdirSync(`./builds`).forEach(file => { 13 | bundleFile(file) 14 | }); 15 | })() 16 | 17 | function bundleFile(file) { 18 | // Based on the filename, give esbuild a specific configuration to build. 19 | ({ 20 | // This output file is meant to be loaded in a browser's