Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                /*@import url('https://fonts.googleapis.com/css?family=Architects+Daughter');
body{font-family:'Architects Daughter';}*/

/*@import url('https://fonts.googleapis.com/css?family=Cabin+Sketch');
body{font-family:'Cabin Sketch';}*/

/*@import url('https://fonts.googleapis.com/css?family=Fredericka+the+Great');
body{font-family:'Fredericka the Great';}*/

@import url('https://fonts.googleapis.com/css?family=Fredoka+One');
body{font-family:'Fredoka One';}

/*@import url('https://fonts.googleapis.com/css?family=Rock+Salt');
body{font-family:'Rock Salt';}*/

/*@import url('https://fonts.googleapis.com/css?family=Shadows+Into+Light+Two');
body{font-family:'Shadows Into Light Two';}*/

/*@import url('https://fonts.googleapis.com/css?family=Short+Stack');
body{font-family:'Short Stack';}*/

/*@import url('https://fonts.googleapis.com/css?family=Sue+Ellen+Francisco');
body{font-family:'Sue Ellen Francisco';}*/

/*@import url('https://fonts.googleapis.com/css?family=Swanky+and+Moo+Moo');
body{font-family:'Swanky and Moo Moo';}*/

/*@import url('https://fonts.googleapis.com/css?family=Waiting+for+the+Sunrise');
body{font-family:'Waiting for the Sunrise';}*/

/*@import url('https://fonts.googleapis.com/css?family=Bad+Script');
body{font-family:'Bad Script';}*/

html,body,#root {
  height: 100%;
}

.vh-100 {
  height: 100vh;
}

.pill {
  border-radius: 2.5rem;
}

pre {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.text-shadow {
  text-shadow: 0 0.25rem 0.75rem #2f4f4f;
}

.top-left {
  position: fixed;
  top: 25px;
  left: 25px;
}

.top-right {
  position: fixed;
  top: 25px;
  right: 25px;
}

.bottom-right {
  position: fixed;
  bottom: 25px;
  right: 25px;
}

.btn-dark {
  color: rgba(255,255,255,.75);
  background: rgba(34,58,40,.5);
  border-color: rgba(34,58,40,.5);
}

.btn-light {
  color: rgba(0,0,0,.75);
  background: rgba(248,249,250,.5);
  border-color: rgba(248,249,250,.5);
}
              
            
!

JS

              
                console.clear()

function Draggable(props) {
  const {children,onDragStart,onDragEnd,value,className=''} = props
  function handleDragStart(e) {
    if (typeof(onDragStart)==='function') onDragStart(e,value)
  }
  function handleDragEnd(e) {
    if (typeof(onDragEnd)==='function') onDragEnd(e)
  }
  return (
    <div onDragStart={handleDragStart} onDragEnd={handleDragEnd} className={className} draggable>{children}</div>
  )
}

function Droppable(props) {
  const {children,onDragOver,onDrop,className='',value} = props
  function handleDragOver(e) {
    e.preventDefault()
    if (typeof(onDragOver)==='function') onDragOver(e)
  }
  function handleDrop(e) {
    e.preventDefault()
    if (typeof(onDrop)==='function') onDrop(e,value)
  }
  return (
    <div onDragOver={handleDragOver} onDrop={handleDrop} className={className}>{children}</div>
  )
}

const TITLE = 'ReactJS: Upload list of images'
const LEAD = 'Drag and drop image to another image to change the order'
const DEG = randInt(360)
const BG = [randColor(),randColor()]
const FLEX = false

const LIST = [...Array(6).keys()].map(e=>{
  return `https://picsum.photos/125?${randInt()}`
})

function App() {
  
  const [index,setIndex] = React.useState()
  const [list,setList] = React.useState([...LIST])
  const btn = React.useRef()
  
  function handleUpload(e) {
    if (typeof(btn)==='object'&&btn!==null) {
      const {current} = btn
      if (typeof(current)==='object'&&current!==null) {
        current.click()
      }
    }
  }
  
  function handleChange(e) {
    e.preventDefault()
    setList([...list,URL.createObjectURL(e.target.files[0])])
  }
  
  function handleDragStart(e,i) {
    setIndex(i)
  }

  function handleDragEnd(e) {
    setIndex()
  }

  function handleDragOver(e) {

  }

  function handleDrop(e,i) {
    if (typeof(index)==='number'&&typeof(i)==='number') {
      const a = index<i?index:i
      const b = index<i?i:index
      setList([...list.slice(0,a), list[b], ...list.slice(a+1,b), list[a], ...list.slice(b+1)])
    }
  }
  
  return (
    <FlexContainer deg={DEG} bg={BG} flex={FLEX}>
      <div className="container">
        <div className="h1 text-center text-white text-shadow">{TITLE}</div>
        <p className="lead text-center text-light text-shadow">{LEAD}</p>
        <div className="p-3 bg-white rounded shadow">
          <div className="text-center mb-3">
            <button className="btn btn-success btn-sm pl-3 pr-3 rounded-pill shadow" onClick={handleUpload}>Upload</button>
            <input className="d-none" type="file" ref={btn} onChange={handleChange} />
          </div>
          
          <div className="form-row">
            {list.map((e,i)=>{
              return (
                <div className="col-4">
                  <Droppable onDrop={handleDrop} onDragOver={handleDragOver} value={i}>
                    <Draggable onDragStart={handleDragStart} onDragEnd={handleDragEnd} value={i}>
                      <img key={i} src={e} className="w-100 border p-1 m-2 shadow-sm" style={{maxHeight:150,objectFit:'contain'}} />
                    </Draggable>
                  </Droppable>
                </div>
              )
            })}
          </div>
          
        </div>
      </div>
    </FlexContainer>
  )
}

function FlexContainer(props) {
  const {children,deg=randInt(360),bg,style={},flex=true} = props
  const classNames = ['align-content-center align-items-center justify-content-center flex-wrap w-100 v-100 vh-100']
  if (Array.isArray(bg)&&bg.length>0) {
    style.background = `linear-gradient(${deg}deg,${bg.join(',')})`
  }
  if (flex) classNames.push('d-flex')
  return (
    <div className={classNames.join(' ')} style={style}>{children}</div>
  )
}

function Details(props) {
  const {summary='details',children,className=''} = props
  return (
    <details className={className}><summary>{summary}</summary><div className="mt-3 animated faster slideInDown">{children}</div></details>
  )
}

function PreCode(props) {
  const {ugly,className='',children} = props
  
  if (typeof(children)==='object'&&children!==null) {
    if (ugly) {
      return (
        <PreCode {...props}>{JSON.stringify(children)}</PreCode>
      )
    }
    return (
      <PreCode {...props}>{JSON.stringify(children,null,2)}</PreCode>
    )
  }
  
  return (
    <pre className={`p-3 bg-dark text-white rounded shadow ${className}`}><code>{children}</code></pre>
  )
}

function randInt(n=10) {
  return Math.floor(Math.random()*n)
}

function randColor() {
  return `#${[...Array(3).keys()].map(e=>(randInt(238)+17).toString(16)).join('')}`
}

ReactDOM.render(
  <App/>,
  document.getElementById('root')
);
              
            
!
999px

Console