Git Product home page Git Product logo

fetch-components's Introduction

HTML Components

Write simple, reusable HTML components in the style of React and Vue ๐Ÿš€

Using with a CDN

<script
  defer
  src="https://unpkg.com/fetch-components@latest/dist/render.min.js"
></script>

<script>
  document.addEventListener('DOMContentLoaded', () => {
    renderHtml.renderComponent('card', '/components/card.html')
  })
</script>

Using with a Package Manager

yarn add -D fetch-components

npm install -D fetch-components
import renderHtml from 'fetch-components'

document.addEventListener('DOMContentLoaded', () => {
  renderHtml.renderComponent('card', '/components/card.html')
})

How it Works

Create a HTML element with the data-render attribute.

<div data-render="card"> </div>

This is the name of the component and is required when rendering.

Add the props you want to pass to the component via data-render-<prop> attributes.

<div
  data-render="card"
  data-render-title="Get Started"
  data-render-text="Join 1000+ happy customers"
  data-render-href="/join"
>
</div>

Add the component file to the root of your project.

- builds
- src
- components
  - card.html

Create the component HTML.

<a href="{{renderHref}}">
  <h5>{{renderTitle}}</h5>
  <p>{{renderText}}</p>
</a>

Note the lack of spacing between {{renderX}}, that is required.

Render the HTML with the renderComponent method, passing in the component name and file.

document.addEventListener('DOMContentLoaded', () => {
  renderHtml.renderComponent('card', '/components/card.html')
})

Nested Components

If you have nested components then you'll want to use the waitFor function that comes with the package.

Using with a CDN

<script>
  document.addEventListener('DOMContentLoaded', () => {
    renderHtml.renderComponent('parent', '/components/parent.html')

    renderHtml.waitFor('parent').then(() => {
      renderHtml.renderComponent('child', '/components/child.html')

      renderHtml
        .waitFor('child')
        .then(() =>
          renderHtml.renderComponent('subchild', '/components/subchild.html')
        )
    })
  })
</script>

Using with a Package Manager

import renderHtml from 'fetch-components'

document.addEventListener('DOMContentLoaded', () => {
  renderHtml.renderComponent('parent', '/components/parent.html')

  renderHtml.waitFor('parent').then(() => {
    renderHtml.renderComponent('child', '/components/child.html')

    renderHtml
      .waitFor('child')
      .then(() =>
        renderHtml.renderComponent('subchild', '/components/subchild.html')
      )
  })
})

Stats ๐Ÿ“Š

fetch-components's People

Contributors

markmead avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.