Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
gnat authored Sep 24, 2023
1 parent f5c6992 commit c89e712
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ For devs who love ergonomics! You may appreciate Surreal if:
* `me()` or `any()` can chain with any Surreal function.
* `me()` can be used directly as a single element (like `querySelector()` or `$()`)
* `any()` can use: `for` / `forEach` / `filter` / `map` (like `querySelectorAll()` or `$()`)
* ๐ŸŒ— No forced style: `class_add` is just an alias of `classAdd`
* ๐ŸŒ— No forced style. Use: `class_add` or `classAdd` or `add_class` or `addClass`
* Use `camelCase` (Javascript) or `snake_case` (Python, Rust, PHP, Ruby, SQL, *CSS*).

### ๐Ÿค” Why use `me()` / `any()` instead of `$()`
Expand Down Expand Up @@ -200,72 +200,72 @@ Looking for [DOM Selectors](#selectors)?
### ๐Ÿงญ Legend
* ๐Ÿ”— Chainable off `me()` and `any()`
* ๐ŸŒ Global convenience helper.
* ๐Ÿ Runnable example.
* โ–ถ๏ธ Runnable example.
* ๐Ÿ”Œ Built-in Plugin
### ๐Ÿ‘๏ธ At a glance

* ๐Ÿ”— `run`
* It's `forEach` but less wordy and works on single elements, too!
* ๐Ÿ `me().run(el => { alert(el) })`
* ๐Ÿ `any('button').run(el => { alert(el) })`
* โ–ถ๏ธ `me().run(el => { alert(el) })`
* โ–ถ๏ธ `any('button').run(el => { alert(el) })`
* ๐Ÿ”— `remove`
* ๐Ÿ `me().remove()`
* ๐Ÿ `any('button').remove()`
* ๐Ÿ”— `classAdd` or `class_add`
* ๐Ÿ `me().classAdd('active')`
* โ–ถ๏ธ `me().remove()`
* โ–ถ๏ธ `any('button').remove()`
* ๐Ÿ”— `classAdd` ๐Ÿ” `class_add` ๐Ÿ” `addClass` ๐Ÿ” `add_class`
* โ–ถ๏ธ `me().classAdd('active')`
* Leading `.` is optional for all class functions, to prevent typical syntax errors with `me()` and `any()`.
* `me().classAdd('active')` and `me().classAdd('.active')` are equivalent.
* ๐Ÿ”— `classRemove` or `class_remove`
* ๐Ÿ `me().classRemove('active')`
* ๐Ÿ”— `classToggle` or `class_toggle`
* ๐Ÿ `me().classToggle('active')`
* These are the same: `me().classAdd('active')` ๐Ÿ” `me().classAdd('.active')`
* ๐Ÿ”— `classRemove` ๐Ÿ” `class_remove` ๐Ÿ” `removeClass` ๐Ÿ” `remove_class`
* โ–ถ๏ธ `me().classRemove('active')`
* ๐Ÿ”— `classToggle` ๐Ÿ” `class_toggle` ๐Ÿ” `toggleClass` ๐Ÿ” `toggle_class`
* โ–ถ๏ธ `me().classToggle('active')`
* ๐Ÿ”— `styles`
* ๐Ÿ `me().styles('color: red')` Add style.
* ๐Ÿ `me().styles({ 'color':'red', 'background':'blue' })` Add multiple styles.
* ๐Ÿ `me().styles({ 'background':null })` Remove style.
* ๐Ÿ”— `attribute` or `attributes` or `attr`
* Get: ๐Ÿ `me().attribute('data-x')`
* โ–ถ๏ธ `me().styles('color: red')` Add style.
* โ–ถ๏ธ `me().styles({ 'color':'red', 'background':'blue' })` Add multiple styles.
* โ–ถ๏ธ `me().styles({ 'background':null })` Remove style.
* ๐Ÿ”— `attribute` ๐Ÿ” `attributes` ๐Ÿ” `attr`
* Get: โ–ถ๏ธ `me().attribute('data-x')`
* Get is only for single elements. For many, wrap the call in `any(...).run(...)` or `any(...).forEach(...)`.
* Set: ๐Ÿ`me().attribute('data-x', true)`
* Set multiple: ๐Ÿ `me().attribute({ 'data-x':'yes', 'data-y':'no' })`
* Remove: ๐Ÿ `me().attribute('data-x', null)`
* Remove multiple: ๐Ÿ `me().attribute({ 'data-x': null, 'data-y':null })`
* Set: โ–ถ๏ธ`me().attribute('data-x', true)`
* Set multiple: โ–ถ๏ธ `me().attribute({ 'data-x':'yes', 'data-y':'no' })`
* Remove: โ–ถ๏ธ `me().attribute('data-x', null)`
* Remove multiple: โ–ถ๏ธ `me().attribute({ 'data-x': null, 'data-y':null })`
* ๐Ÿ”— `trigger`
* ๐Ÿ `me().trigger('hello')`
* โ–ถ๏ธ `me().trigger('hello')`
* Wraps `dispatchEvent`
* ๐Ÿ”— `on`
* ๐Ÿ `me().on('click', ev => { me(ev).styles('background', 'red') })`
* โ–ถ๏ธ `me().on('click', ev => { me(ev).styles('background', 'red') })`
* Wraps `addEventListener`
* ๐Ÿ”— `off`
* ๐Ÿ `me().remove('click')`
* โ–ถ๏ธ `me().remove('click')`
* Wraps `removeEventListener`
* ๐Ÿ”— `offAll`
* ๐Ÿ `me().offAll()`
* โ–ถ๏ธ `me().offAll()`
* ๐ŸŒ `sleep`
* ๐Ÿ `await sleep(1000, ev => { alert(ev) })`
* โ–ถ๏ธ `await sleep(1000, ev => { alert(ev) })`
* `async` version of `setTimeout`
* Wonderful for animation timelines.
* ๐ŸŒ `tick`
* ๐Ÿ `await tick()`
* โ–ถ๏ธ `await tick()`
* `await` version of `rAF` / `requestAnimationFrame`.
* Animation tick. Waits 1 frame.
* Great if you need to wait for events to propagate.
* ๐ŸŒ `rAF`
* ๐Ÿ `rAF(e => { return e })`
* โ–ถ๏ธ `rAF(e => { return e })`
* Animation tick. Fires when 1 frame has passed. Alias of [requestAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)
* Great if you need to wait for events to propagate.
* ๐ŸŒ `rIC`
* ๐Ÿ `rIC(e => { return e })`
* โ–ถ๏ธ `rIC(e => { return e })`
* Great time to compute. Fires function when JS is idle. Alias of [requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback)
* ๐ŸŒ `halt`
* ๐Ÿ `halt(event)`
* โ–ถ๏ธ `halt(event)`
* Great to prevent default browser behavior: such as displaying an image vs letting JS handle it.
* Wrapper for [preventDefault](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
* ๐ŸŒ `createElement` or `create_element`
* ๐Ÿ `el_new = createElement("div"); me().prepend(el_new)`
* Alias of `document.createElement`
* ๐ŸŒ `onloadAdd` or `onload_add`
* ๐Ÿ `onloadAdd(_ => { alert("loaded!"); })`
* ๐ŸŒ `createElement` ๐Ÿ” `create_element`
* โ–ถ๏ธ `el_new = createElement("div"); me().prepend(el_new)`
* Alias of vanilla `document.createElement`
* ๐ŸŒ `onloadAdd` ๐Ÿ” `onload_add` ๐Ÿ” `addOnload` ๐Ÿ” `add_onload`
* โ–ถ๏ธ `onloadAdd(_ => { alert("loaded!"); })`
* Execute after the DOM is ready. Similar to jquery `ready()`
* Queues functions onto `window.onload`
* Why? So you don't overwrite `window.onload`, also predictable sequential loading!
Expand All @@ -275,16 +275,16 @@ Looking for [DOM Selectors](#selectors)?
#### Effects
Build your own effects with `me().styles({...})` then timelining with [CSS transitions using `await` or callbacks](#timelines). We ship some common effects:

* ๐Ÿ”— `fadeOut` or `fade_out`
* ๐Ÿ”— `fadeOut` ๐Ÿ” `fade_out`
* Fade out and remove element.
* Keep element with `remove=false`.
* ๐Ÿ `me().fadeOut()`
* ๐Ÿ `me().fadeOut(ev => { alert("Faded out!") }, 3000)` Over 3 seconds then call function.
* โ–ถ๏ธ `me().fadeOut()`
* โ–ถ๏ธ `me().fadeOut(ev => { alert("Faded out!") }, 3000)` Over 3 seconds then call function.

* ๐Ÿ”— `fadeIn` or `fade_in`
* ๐Ÿ”— `fadeIn` ๐Ÿ” `fade_in`
* Fade in existing element which has `opacity: 0`
* ๐Ÿ `me().fadeIn()`
* ๐Ÿ `me().fadeIn(ev => { alert("Faded in!") }, 3000)` Over 3 seconds then call function.
* โ–ถ๏ธ `me().fadeIn()`
* โ–ถ๏ธ `me().fadeIn(ev => { alert("Faded in!") }, 3000)` Over 3 seconds then call function.


### <a name="no-surreal"></a>๐Ÿ”ฎ No Surreal Needed
Expand All @@ -293,27 +293,27 @@ More often than not, Vanilla JS is the easiest way!

Logging
* ๐ŸŒ `console.log()` `console.warn()` `console.error()`
* Event logging: ๐Ÿ `monitorEvents(me())` See: [Chrome Blog](https://developer.chrome.com/blog/quickly-monitor-events-from-the-console-panel-2/)
* Event logging: โ–ถ๏ธ `monitorEvents(me())` See: [Chrome Blog](https://developer.chrome.com/blog/quickly-monitor-events-from-the-console-panel-2/)

Benchmarking / Time It!
* ๐Ÿ `console.time('name')`
* ๐Ÿ `console.timeEnd('name')`
* โ–ถ๏ธ `console.time('name')`
* โ–ถ๏ธ `console.timeEnd('name')`

Text / HTML Content
* ๐Ÿ `me().textContent = "hello world"`
* โ–ถ๏ธ `me().textContent = "hello world"`
* XSS Safe! See: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
* ๐Ÿ `me().innerHTML = "<p>hello world</p>"`
* ๐Ÿ `me().innerText = "hello world"`
* โ–ถ๏ธ `me().innerHTML = "<p>hello world</p>"`
* โ–ถ๏ธ `me().innerText = "hello world"`

Children
* ๐Ÿ `me().children`
* ๐Ÿ `me().children.hidden = true`
* โ–ถ๏ธ `me().children`
* โ–ถ๏ธ `me().children.hidden = true`

Append / Prepend elements.
* ๐Ÿ `me().prepend(el_new)`
* ๐Ÿ `me().appendChild(el_new)`
* ๐Ÿ `me().insertBefore(el_new, el.firstChild)`
* ๐Ÿ `me().insertAdjacentHTML("beforebegin", el_new)`
* โ–ถ๏ธ `me().prepend(el_new)`
* โ–ถ๏ธ `me().appendChild(el_new)`
* โ–ถ๏ธ `me().insertBefore(el_new, el.firstChild)`
* โ–ถ๏ธ `me().insertAdjacentHTML("beforebegin", el_new)`

## ๐Ÿ’Ž Conventions & Tips

Expand Down

0 comments on commit c89e712

Please sign in to comment.