├── .gitignore ├── .babelrc ├── .eslintrc.json ├── src └── index.js ├── webpack.config.js ├── README.md ├── package.json └── dist └── stimulus-existence.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | package-lock.json -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["transform-class-properties", "@babel/transform-runtime"], 3 | "presets": [ 4 | ["@babel/preset-env", { 5 | "modules": false 6 | }] 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es6": true, 5 | "node": true 6 | }, 7 | "extends": "standard", 8 | "globals": { 9 | "Atomics": "readonly", 10 | "SharedArrayBuffer": "readonly" 11 | }, 12 | "parser": "babel-eslint", 13 | "parserOptions": { 14 | "sourceType": "module", 15 | "allowImportExportEverywhere": false, 16 | "ecmaFeatures": { 17 | "globalReturn": false 18 | } 19 | }, 20 | "rules": {} 21 | } 22 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import { Controller } from "stimulus"; 2 | 3 | export class ExistenceController extends Controller { 4 | connect() { 5 | this.element.dispatchEvent( 6 | new Event("existence:added", { 7 | bubbles: true, 8 | cancelable: false, 9 | }) 10 | ); 11 | } 12 | 13 | remove() { 14 | this.element.dispatchEvent( 15 | new Event("existence:removed", { 16 | bubbles: true, 17 | cancelable: false, 18 | }) 19 | ); 20 | 21 | this.element.remove(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require("path"); 2 | 3 | module.exports = { 4 | entry: "./src/index.js", 5 | output: { 6 | path: path.resolve(__dirname, "dist"), 7 | filename: "stimulus-existence.js", 8 | library: "stimulusExistence", 9 | libraryTarget: "umd", 10 | }, 11 | externals: { 12 | stimulus: { 13 | commonjs: "stimulus", 14 | commonjs2: "stimulus", 15 | amd: "stimulus", 16 | root: "stimulus", 17 | }, 18 | }, 19 | module: { 20 | rules: [ 21 | { 22 | test: /\.js$/, 23 | include: path.resolve(__dirname, "src"), 24 | exclude: [/node_modules/], 25 | use: [{ loader: "babel-loader" }], 26 | }, 27 | ], 28 | }, 29 | }; 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Stimulus Existence 2 | 3 | Stimulus controller to remove elements from the dom, and be notified with an event when they are added. 4 | 5 | ## Installation 6 | 7 | ```shell 8 | $ yarn add stimulus-existence 9 | ``` 10 | 11 | ## Usage 12 | 13 | Register the controller with Stimulus: 14 | 15 | ```javascript 16 | // application.js 17 | import { Application } from "stimulus"; 18 | import { ExistenceController } from "stimulus-existence"; 19 | 20 | const application = Application.start(); 21 | application.register("existence", ExistenceController); 22 | ``` 23 | 24 | Initialize the controller on a container element, use the `remove()` action to delete the entire container. Listen to `existence:added` events to discover when your container has been added to the dom. Useful for Rails Ujs Dom HTML additions, and hooking up with other controllers. 25 | 26 | ```html 27 |