qiutian00 / only-show

An element that shows or hides based on provided conditions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

only-show

A Web Component for showing or hiding content when given conditions are met.

Demo

Examples

General usage example:

<script type="module" src="only-show.js"></script>

<only-show with-param="edit">
  This will be visible by default,
  but will hide
  if the current url
  does not have
  the `edit` query parameter attached.
</only-show>

<only-show with-support="container-name: any" hidden>
  This will be hidden by default,
  but show up in browsers
  that support container queries.
</only-show>

Example using has-any option:

<script type="module" src="only-show.js"></script>

<only-show with-hash="slide-2" with-param="show_all" has-any>
  This will be visible
  if either the hash or query parameter
  is present in the URL
</only-show>

ToDo

  • Add support for container queries
  • Watch for resize and hashChange and other events

Installation

You have a few options (choose one of these):

  1. Install via npm: npm install @oddbird/only-show
  2. Download the source manually from GitHub into your project.
  3. Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)

Usage

Make sure you include the <script> in your project (choose one of these):

<!-- Host yourself -->
<script type="module" src="only-show.js"></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://www.unpkg.com/@oddbird/only-show@1.0.0/only-show.js"
></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://esm.sh/@oddbird/only-show@1.0.0"
></script>

Credit

With thanks to the following people:

Support

At OddBird, we enjoy collaborating and contributing as part of an open web community. But those contributions take time and effort. If you're interested in supporting our open-source work, consider becoming a GitHub sponsor, or contributing to our Open Collective.

About

An element that shows or hides based on provided conditions

License:MIT License


Languages

Language:JavaScript 61.8%Language:HTML 38.2%