Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

              
                <h1>Access image colors as metadata in <a href="https://sanity.io/?utm_source=css-tricks&utm_campaign=csstricks">sanity.io</a></h1>
<p>With image colors as metadata you can decide which contrast color for overlaying text. You can also use the color palette for other parts of your design.</p>
<div id="app">
  <span id="loading">Loading...</span>
</div>
<footer>
  <p>This is a demo using structured content in <a href="https://sanity.io/?utm_source=css-tricks&utm_campaign=csstricks">sanity.io</a></p>
  <p>Photos from <a href="https://unsplash.com">unsplash.com</a></p>
</footer>
              
            
!

CSS

              
                :root {
  --primary-color: #aef;
  --background-color: #1d1e22;
  --border-radius: 3px;
}
body {
  padding: 1em;
  background: var(--background-color);
  color: #fff;
}
a {
  color: var(--primary-color);
}
pre {
  font-size: 11px;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

img {
  width: 100%;
}

button {
  position: relative;
  font-size: 1em;
  display: inline-block;
  padding: 0.5em;
  border: 2px solid currentColor;
  background-color: #fff;
  margin-top: 1em;
  border-radius: var(--border-radius);
}

.metadata {
  background: #fff;
  color: black;
  width: auto;
  margin: 1em;
  padding: 1em;
  top: 1em;
}

.container {
  display: flex;
  flex-wrap: wrap;
}

.card {
  background-color: #fff;
  color: #000;
  flex-basis: 30%;
  margin: 3px;
  padding: 5px;
  border-radius: var(--border-radius);
}
@media only screen and (max-width: 600px) {
  .card {
      flex-basis: 100%;
  }
}
figure {
  margin: 0;
  display: block;
  position: relative;
}
.figure__overlay {
  position: absolute;
  text-transform: uppercase;
  font-weight: bold;
  font-size: 10vw;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ReactModal__Overlay--after-open {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
  max-height: 100vh;
  overflow-y: auto
}
              
            
!

JS

              
                ReactModal.setAppElement('#app')

const client = SanityClient({
  projectId: "anokeucs",
  dataset: "production",
  useCdn: true
})
const query = `*[_type == 'post' && defined(mainImage)]{ 
    ...,
    "mainImage": mainImage.asset->
  }`

class ImageExample extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      result: [],
      showModal: ''
    }
    this.openDialog = this.openDialog.bind(this)
    this.closeDialog = this.closeDialog.bind(this)
  }
  openDialog(_id) {
    this.setState({ showModal: _id })
  }
  closeDialog(_id) {
    this.setState({ showModal: '' })
  }

  componentDidMount() {
    client.fetch(query).then((result = []) => this.setState({ result }))
  }
  render() {
    const { result = [] } = this.state
    if (!result.length) return <div>Loading...</div>
    return (
      <div className="container">
        {result.map(({ _id, mainImage }) => (
          <div
            key={_id}
            className="card"
            style={{
              backgroundColor: mainImage.metadata.palette.dominant.background,
              color: mainImage.metadata.palette.dominant.foreground
            }}
          >
            <figure>
              <img src={`${mainImage.url}?w=800`} />
              <div className="figure__overlay">
                {mainImage.metadata.palette.dominant.foreground}
              </div>
            </figure>
            <button onClick={() => this.openDialog(_id)}>
              Inspect metadata
            </button>
            <ReactModal
              isOpen={this.state.showModal === _id}
              contentLabel="Minimal Modal Example"
              id={_id}
              OnAfterClose={() => document.body.style.overflow = 'hidden' }
OnRequestClose={() => document.body.removeAttribute('style') }
              className="metadata"
            >
              <button onClick={() => this.closeDialog(_id)}>Close</button>

              <pre>{JSON.stringify(mainImage, null, 2)}</pre>
            </ReactModal>
          </div>
        ))}
      </div>
    )
  }
}

ReactDOM.render(<ImageExample />, document.getElementById("app"))

              
            
!
999px

Console