npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-sometimes

v1.0.1

Published

React component to conditionally render content based on a date/time range.

Downloads

6

Readme

react-sometimes

This is a React component which can be used to conditionally render content based on a date/time range.

Installation

Install react-sometimes using your favourite package manager.

npm

npm install react-sometimes

pnpm

pnpm add react-sometimes

yarn

yarn add react-sometimes

Usage

Import the component from react-sometimes.

import Sometimes from 'react-sometimes'

The from and to Properties

The component accepts from and to properties which define a time period when the content should be rendered. Both are optional and can be specified as either a date or date and time in ISO 8601 format, e.g. YYYY-MM-DD or YYYY-MM-DD HH:MM::SS (the T separating date and time is optional). Any of the other string formats supported by badger-timestamp can also be used.

Use the from property to render some content after a particular date or date/time combination.

<Sometimes from="2023-01-01 16:20:00">
  This content will only be display on or after 4:20pm on January 1st 2023.
</Sometimes>

If you don't specify a time then it is assumed to be 00:00:00.

<Sometimes from="2023-01-01">
  This content will only be display from 2023 onwards.
</Sometimes>

Use the to property to render some content up to a particular date or date/time combination. If you don't specify a time then it is assumed to be 00:00:00. The implication of this is that the content will NOT be rendered once that date is reached.

<Sometimes to="2024-01-01">
  This content will only be displayed before the start of 2024.
</Sometimes>

You can specify a time explicitly for the cut-off point.

<Sometimes to="2024-01-01 11:59:59">
  This content will NOT be display after noon on the first day of 2024.
</Sometimes>

You can specify both from and to.

<Sometimes from="2023-12-01 00:00:00" to="2023-12-31 23:59:59">
  This content will only be displayed in December 2023.
</Sometimes>

If you don't specify either from or to then the content will always be displayed. This is a pointless exercise, but it's possible that you might be defining the from and/or to constraints somewhere else (e.g. in a configuration file) where one, either or both could possibly be undefined. The component will attempt to do the right thing.

Both properties are inclusive. So the content will be displayed as soon as the from time is reached and up to, and including, the to time. However, note the warning above that the default time is assumed to be 00:00:00 if you only specify a date. So when a date is used as the to time the content will STOP being displayed as soon as that date is reached.

Wildcard Dates

You can use * as a wildcard for the date in either from or to if you want to target a particular time range on all days.

For example, to display some content between 09:00:00 and 17:00:00 every day:

<Sometimes from="* 09:00:00" to="* 17:00:00">
  We're open!  Call us on 555-1234.
</Sometimes>

The inside and outside Properties

Instead of defining the content you want to display as children of the component, you can define it using the inside property. The content will be displayed when the current time is inside the specified range.

<Sometimes
  from="2023-12-01 00:00:00"
  to="2023-01-01 11:59:59"
  inside={<DecemberOffers/>}
/>

You can also use the outside property as the inverse of inside. This content will be displayed when the current time is outside the specified range.

<Sometimes
  from="2022-12-22 17:00:00"
  to="2023-01-04 09:00:00"
  inside={<ClosedForXmas/>}
  outside={<OpeningHours/>}
/>

Author

Andy Wardley