Welcome to pipe-dom 👋
DOM manipulation with the F#-style pipeline operator
Install
pipe-dom is intended to be used with the F#-style pipeline operator.
npm install --save pipe-dom @babel/core @babel/plugin-proposal-pipeline-operator
Configure your .babelrc
:
Philosophy
-
Experimental: This library is intended to be used in combination with the pipeline operator, a Stage 1 proposal. Because that proposal is experimental & in flux, this library will be too, as we want to be able to adjust the API of the library to align with pipeline best practices. This library will follow semver in doing so, but breaking changes are possible, if not likely. This will change if the pipeline operator is accepted.
-
Modern Browsers: While we aim to normalize API differences across modern browsers, we are not going to support old browsers (cough IE11 cough). We will use modern DOM API methods internally. If your target browsers do not support those methods, they should be polyfilled by the consumer, but because of the experimental nature of the library, we don't foresee this being a problem. This also could change if the pipeline operator is accepted.
-
Expansive: While the initial release has a small API surface, any useful methods are welcome to be added. Because it exports functions, the library is tree-shakeable, which means extra methods have little to no impact on consumer bundle size.
-
Arrays Everywhere:
queryAll
returns an array rather than a NodeList, and themap
implementation will returns an array. This is a cleanest primitive to target and work with.NodeList
s are to be avoided. -
Impure: This is not inteded to be a functional library, but a fluent one a la jQuery. Because we're dealing with the DOM, Nodes will be mutated. This can produce some weird behavior, like this:
|> ;This will result in
someNode
attached to the last element in the list, as the reference tosomeNode
remains the same across all iterations. Appending each time will movesomeNode
to the next element in the list. If you want to create a new node on every iteration, try this:|> ;
API
query
;
Query for a single element by the given selector.
Example
;
queryAll
;
Query for an array of elements by the given selector.
Example
;
map
;
A curried map
function to use with an array of Nodes.
Example
|> ;
append
;
Append Nodes as the last children to the given Node.
Example
|> ;
prepend
;
Append Nodes as the first child to the given Node.
Example
|> ;
insertBefore
;
Insert Nodes before relative child of target Node.
Example
|> ;
insertAfter
;
Insert Nodes after relative child of target Node.
Example
|> ;
replace
;
Replace an existing child Node with a new child Node for the target Node.
Example
|> ;
remove
;
Remove a child Node from a given Node.
Example
|> ;
contains
;
Determines whether one Node is contained in another Node.
Example
|> ;
setAttribute
;
Sets the attribute to a given value on the target Node.
Example
|> ;
getAttribute
;
Get an attribute from the target Node.
Example
|> ;
addClass
;
Adds a class to a target Node.
Example
|> ;
removeClass
;
Removes a class from a target Node.
Example
|> ;
toggleClass
;
Toggles a class on a target Node. If toggle is provided, class will be in provided state (on for true
, off for false
). If toggle not provided, class state will be swapped from current (added if missing, removed if present).
Example
|> ; |> ; |> ;
on
;
Adds an event listener to the target Node.
Example
|> ;
off
;
Removes an event listener from the target Node.
Example
const handler = console; |> ;
Author
👤 James DiGioia jamesorodig@gmail.com (http://jamesdigioia.com)
- Twitter: @JamesDiGioia
- Github: @mAAdhaTTah
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Run tests
npm test
Show your support
Give a ⭐️ if this project helped you!
📝 License
Copyright © 2019 James DiGioia jamesorodig@gmail.com (http://jamesdigioia.com).
This project is MIT licensed.