Golfed Codepen – 3D Spiral Thing
copy // sort of golfed version of https://www.instagram.com/p/C1uv6Kqv19T/ // by @mewtru b = document.body a = Object .assign a( b.style , { background: '#000' , color: '#fff' } ) w = 'snippet.zone snippet.zone' .toUpperCase ( ) .split `` F = ( n, O = 0 , C, S, o, t) => { b.innerHTML += `< div id= ${ n} style= 'position:absolute;left:50%;top:50%;translate:-50% -50%;width:100% text-align:center;white-space:nowrap' ></ div> ` w.map ( l => this [ n] .innerHTML += `< span style= 'display:inline-block;margin-right:5px;font-size:28px' > ${ l} </ span> `) t = O setInterval( _ => { t += .005 ; [ ...this [ n] .children ] .map ( ( e, i) => { T = t + i / 2.7 a( e.style , { translate: `0 ${ Math .sin ( T) * 100 } px`, scale: Math .cos ( T) * .5 + .5} ) } , 16 ) } ) } F( 'Z' ) F( 'X' , 3 )
Try it out…
“Very cool” pen by Lucas Fernando that comes from @mewtru
I decided to do a speed-coded semi-golfed version… can definitely be way more golfed 😀
Simple Way to Validate CSS Colors
copy function isColor( col) { const cache = isColor[ col] if ( cache != null ) { console.log ( '- reading cache' ) return cache } isColor.el .style = '' isColor.el .style .color = col return isColor[ col] = !! isColor.el .style .color } isColor.el = document.createElement ( 'div' ) console.log ( 'is "red" a color?' , isColor( 'red' ) ) console.log ( 'from the cache: ' , isColor( 'red' ) ) console.log ( 'is "rgbx(1, 2, 3)" a color?' , isColor( 'rgbx(1, 2, 3)' ) ) console.log ( 'is "#0f0" a color?' , isColor( '#0f0' ) ) console.log ( 'is "hsl(192, 50%, 50%)" a color?' , isColor( 'hsl(192, 50%, 50%)' ) ) console.log ( 'from the cache: ' , isColor( 'hsl(192, 50%, 50%)' ) ) console.log ( 'is "lab(2000.1337% -8.6911 -159.131231 / .987189732)" ?' , isColor( 'lab(2000.1337% -8.6911 -159.131231 / .987189732)' ) ) console.log ( 'is "snippetZone" ?' , isColor( 'snippetZone' ) )
Try it out…
I find this technique is usually good enough to validate colors…
Remove a DOM HTML Element
copy const button = document.createElement ( 'button' ) ; button.innerText = 'Hello There' ; document.body .appendChild ( button) ; // click anywhere document.body .addEventListener ( 'click' , ( ) => { if ( button.parentNode != null ) { button.parentNode .removeChild ( button) ; } } ) ;
Try it out…
In vanilla js you’ll find yourself checking if an HTML DOM element can be removed, by seeing if it has a parent (as seen above). Forgetting to do so is the source of many errors.
With the death of IE11 you can use remove()
copy const button = document.createElement ( 'button' ) ; button.innerText = 'Hello There 2' ; document.body .appendChild ( button) ; // click anywhere document.body .addEventListener ( 'click' , ( ) => { button.remove ( ) ; } ) ;
Try it out…
No error occurs when calling remove()
on something that is already removed… just like the old jQuery days 😉
Mutation Observer
copy // Select the node that will be observed for mutations const targetNode = document.getElementById ( 'some-id' ) ; // Options for the observer (which mutations to observe) const config = { attributes: true , childList: true , subtree: true } ; // Callback function to execute when mutations are observed const callback = function ( mutationsList, observer) { // Use traditional 'for loops' for IE 11 (goodbye IE11!!!!) for ( const mutation of mutationsList) { if ( mutation.type === 'childList' ) { console.log ( 'A child node has been added or removed.' ) ; } else if ( mutation.type === 'attributes' ) { console.log ( 'The ' + mutation.attributeName + ' attribute was modified.' ) ; } } } ; // Create an observer instance linked to the callback function const observer = new MutationObserver( callback) ; // Start observing the target node for configured mutations observer.observe ( targetNode, config) ; // Later, you can stop observing observer.disconnect ( ) ;
This is pure gold if you haven’t used it… (from MDN)
React Vanilla
copy < style>* { font- family: sans- serif; margin- bottoM: .5em; } </ style> < h3> TODO</ h3> < ul id= todos></ ul> < label> What needs to be done?< br> < input id= todo onchange= "newTodo()" />< br> </ label> < button id= add onclick= "newTodo()" > Add #1 </ button> < script> let count = 1 function newTodo( ) { if ( todo.value .length > 0 ) { todos.innerHTML += `< li> ${ todo.value } </ li> ` todo.value = '' add.innerText = `Add #${ ++ count} ` } } </ script>
I don’t really like React… Don’t get me wrong, I don’t mind it and I even kind of like using it – there’s something fun about it… But it’s surprising to me that UI work is still so bulky… I think React and most other UI libraries are overly complex… Every now and then I do evil style vanilla js versions of the React homepage examples as a sort of rebellion 😀 This is the React version of the above snippet:
copy class TodoApp extends React.Component { constructor( props) { super ( props) ; this .state = { items: [ ] , text: '' } ; this .handleChange = this .handleChange .bind ( this ) ; this .handleSubmit = this .handleSubmit .bind ( this ) ; } render( ) { return ( < div> < h3> TODO</ h3> < TodoList items= { this .state .items } /> < form onSubmit= { this .handleSubmit } > < label htmlFor= "new-todo" > What needs to be done? </ label> < input id= "new-todo" onChange= { this .handleChange } value= { this .state .text } /> < button> Add #{ this .state .items .length + 1 } </ button> </ form> </ div> ) ; } handleChange( e) { this .setState ( { text: e.target .value } ) ; } handleSubmit( e) { e.preventDefault ( ) ; if ( this .state .text .length === 0 ) { return ; } const newItem = { text: this .state .text , id: Date .now ( ) } ; this .setState ( state => ( { items: state.items .concat ( newItem) , text: '' } ) ) ; } } class TodoList extends React.Component { render( ) { return ( < ul> { this .props .items .map ( item => ( < li key= { item.id } > { item.text } </ li> ) ) } </ ul> ) ; } } root.render ( < TodoApp /> ) ;