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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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 class="container">
  <h1>Modal with data using Alpine.js</h1>
  <p>Here I have created a simple modal using Alpine.js with the $dispatch magic property and fetch, each button will call a different post from the JSONPlaceholder API.</p>
  <p>$dispatch will send the article id, a value of true and a url to a function. These will help show the modal window and what is displayed within it.</p>

  <div class="buttons">
    <div x-data="{}">
      <div class="button" @click="$dispatch('modal', {articleSearch: 1, isOpen: true,  url: './item-one.html'})">
        Article One
      </div>
    </div>
    <div x-data="{}">
      <div class="button" @click="$dispatch('modal', {articleSearch: 2, isOpen: true, url: './item-two.html'})">
        Article Two
      </div>
    </div>
    <div x-data="{}">
      <div class="button" @click="$dispatch('modal', {articleSearch: 3, isOpen: true,  url: './item-three.html'})">
        Article Three
      </div>
    </div>
  </div>
</div>

<div x-data="articleSearch()">

  <template x-on:modal.window="articleSearch = $event.detail.articleSearch; url = $event.detail.url; isOpen = $event.detail.isOpen; fetchArticle();"></template>
  <template x-if="article">
    <div class="modal-overlay" x-show="isOpen" x-cloak>
      <div class="modal-container" x-show="isOpen" @click.away="closeArticle();" x-cloak>

        <div class="modal-head">
          <div class="article-attributes">
            <span x-text="'Article ' + article.id"></span> | <span x-text="'By user ' + article.userId"></span> | <span>26/05/2020</span>
          </div>
          <div class="close" @click="closeArticle();"><i class="fas fa-times">X</i></div>
        </div>

        <div class="modal-body">
          <h3 x-text="article.title"></h3>
          <p x-text="article.body"></p>
        </div>

      </div>
    </div>
  </template>

</div>
              
            
!

CSS

              
                body {
  background: #2a363b;
}

.container {
  align-items: center;
  box-sizing: border-box;
  color: white;
  display: flex;
  flex-flow: column;
  height: 100%;
  justify-content: center;
  padding: 20px;
  text-align: center;
  width: 100vw;
  > h1 {
    margin-bottom: 10px;
  }
  > p {
    font-size: 20px;
    line-height: 1.5;
    max-width: 900px;
    margin-bottom: 0;
  }
  > .buttons {
    display: inline-flex;
    flex-flow: row wrap;
    justify-content: center;
    margin-top: 30px;
    max-width: 100%;
  }
}

.button {
  background: #ff847c;
  border: 4px solid #ff847c;
  color: white;
  cursor: pointer;
  font-weight: 800;
  letter-spacing: 0.1rem;
  margin: 15px;
  padding: 15px;
  text-align: center;
  width: 150px;

  transition: all 200ms linear;
  &:hover {
    background: transparent;
    color: #ff847c;
  }
}

.modal-overlay {
  align-items: center;
  background: rgba(black, 0.5);
  display: flex;
  height: 100vh;
  justify-items: center;
  left: 0;
  position: fixed;
  top: 0;
  width: 100vw;
  z-index: 99;
}

.modal-container {
  background: white;
  height: 80vh;
  margin: auto;
  overflow: scroll;
  width: 80vw;

  > .modal-head {
    background: #355c7d;
    color: white;
    display: flex;
    justify-content: space-between;
    padding: 15px;
    position: sticky;
    top: 0;
    z-index: 2;
    > .close {
      cursor: pointer;
      font-size: 15px;
      margin-right: -15px;
      text-align: center;
      width: 40px;
    }
  }
  > .modal-body {
    padding: 0 20px;
  }
}

              
            
!

JS

              
                function articleSearch() {
  return {
    isLoading: false,
    isOpen: false,
    articleSearch: 0,
    url: "",
    article: null,
    fetchArticle() {
      this.isLoading = true;

      fetch(`https://jsonplaceholder.typicode.com/posts/${this.articleSearch}`)
        .then((response) => response.json())
        .then((data) => {
          // console.log(data)
          this.isLoading = false;
          this.article = data;

          window.history.pushState("", "", this.url);
        })
        .catch((err) => console.log("ERROR", err));
    },
    closeArticle() {
      window.history.back();
      this.isOpen = false;
      this.articleSearch = 0;
      this.url = "";
      this.article = null;
    }
  };
}

              
            
!
999px

Console