├── .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 |
31 | 32 |
33 | ``` 34 | 35 | ## Contributing 36 | 37 | Fork the project. 38 | 39 | Install dependencies 40 | 41 | ```shell 42 | $ yarn install 43 | ``` 44 | 45 | Write some tests, and add your feature. Send a PR. 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stimulus-existence", 3 | "version": "1.1.1", 4 | "description": "Stimulus controller to remove elements from the dom, and be notified with an event when they are added.", 5 | "main": "dist/stimulus-existence.js", 6 | "scripts": { 7 | "start": "webpack --watch", 8 | "build": "webpack --env dev && webpack --env build" 9 | }, 10 | "keywords": [ 11 | "stimulus", 12 | "existence", 13 | "hide", 14 | "show" 15 | ], 16 | "author": "Joshua Harris ", 17 | "licence": "MIT", 18 | "devDependencies": { 19 | "@babel/core": "^7.10.3", 20 | "@babel/plugin-transform-runtime": "^7.10.3", 21 | "@babel/preset-env": "^7.10.3", 22 | "@babel/runtime": "^7.10.3", 23 | "babel-eslint": "^10.1.0", 24 | "babel-loader": "^8.1.0", 25 | "babel-plugin-transform-class-properties": "^6.24.1", 26 | "eslint": "^7.3.0", 27 | "eslint-config-standard": "^14.1.1", 28 | "eslint-plugin-import": "^2.21.2", 29 | "eslint-plugin-node": "^11.1.0", 30 | "eslint-plugin-promise": "^4.2.1", 31 | "eslint-plugin-standard": "^4.0.1", 32 | "webpack": "^4.43.0", 33 | "webpack-cli": "^3.3.12" 34 | }, 35 | "dependencies": { 36 | "stimulus": "~1.1.1||~2.0" 37 | }, 38 | "bugs": { 39 | "url": "https://gitlab.com/initforthe/stimulus-existence" 40 | }, 41 | "homepage": "https://gitlab.com/initforthe/stimulus-existence", 42 | "repository": { 43 | "type": "git", 44 | "url": "https://gitlab.com/initforthe/stimulus-existence.git" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /dist/stimulus-existence.js: -------------------------------------------------------------------------------- 1 | !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("stimulus")):"function"==typeof define&&define.amd?define(["stimulus"],t):"object"==typeof exports?exports.stimulusExistence=t(require("stimulus")):e.stimulusExistence=t(e.stimulus)}(window,(function(e){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=6)}([function(e,t){function n(t){return e.exports=n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},n(t)}e.exports=n},function(e,t){e.exports=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}},function(e,t){function n(e,t){for(var n=0;n