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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mikesha/flatpickr-year-select-plugin

v0.0.5

Published

A plugin for flatpickr that transforms the date picker into a year selector with a decade-based navigation interface.

Readme

Year Select Plugin for Flatpickr

A plugin for flatpickr that transforms the date picker into a year selector with a decade-based navigation interface.

Support me

If you like what I'm doing, please support me: https://ko-fi.com/mykeshato

Installation

npm install flatpickr
# or
yarn add flatpickr

The plugin is imported separately:

import flatpickr from 'flatpickr';
import yearSelectPlugin from './path-to-yearSelectPlugin';

Basic Usage

import flatpickr from 'flatpickr';
import yearSelectPlugin from './yearSelectPlugin';

flatpickr('#my-input', {
  plugins: [yearSelectPlugin()]
});

Configuration

The plugin accepts an optional configuration object:

interface Config {
  dateFormat: string; // Format for storing date value
  altFormat: string; // Format for displaying date
  theme: string; // Theme for styling ('light' by default)
}

Default Configuration

const defaultConfig = {
  dateFormat: 'Y', // Year format (YYYY)
  altFormat: 'Y', // Display format (YYYY)
  theme: 'light' // Default theme
};

Example with Custom Configuration

flatpickr('#year-picker', {
  plugins: [
    yearSelectPlugin({
      dateFormat: 'Y-m-d',
      altFormat: 'Y',
      theme: 'dark'
    })
  ]
});

Features

Decade Navigation

  • Years are displayed in a decade view (e.g., 2020-2029)
  • Previous button (←) navigates to the previous decade
  • Next button (→) navigates to the next decade

Year Selection

  • Single Mode: Select a single year
  • Range Mode: Select a range of years
  • Multiple Mode: Select multiple years

Keyboard Navigation

The plugin supports keyboard navigation:

  • Arrow keys to navigate between years
  • Enter to select the focused year

| Key | Action | | ---------- | ------------------- | | ArrowLeft | Move left | | ArrowRight | Move right | | ArrowUp | Move up | | ArrowDown | Move down | | Enter | Select focused year |

Examples

Single Year Selection

import flatpickr from 'flatpickr';
import yearSelectPlugin from './yearSelectPlugin';

flatpickr('#year-single', {
  plugins: [yearSelectPlugin()],
  mode: 'single'
});

Year Range Selection

import flatpickr from 'flatpickr';
import yearSelectPlugin from './yearSelectPlugin';

flatpickr('#year-range', {
  plugins: [yearSelectPlugin()],
  mode: 'range'
});

With Min and Max Date Constraints

import flatpickr from 'flatpickr';
import yearSelectPlugin from './yearSelectPlugin';

flatpickr('#year-constrained', {
  plugins: [yearSelectPlugin()],
  minDate: '2010-01-01',
  maxDate: '2030-12-31'
});

Styling

The plugin adds the class flatpickr-yearSelect-theme-{theme} to the calendar container, where {theme} is the theme specified in the config (defaults to 'light').

CSS Classes

The plugin uses the following CSS classes:

  • flatpickr-yearSelect-years: Container for year elements
  • flatpickr-yearSelect-year: Individual year element
  • flatpickr-yearSelect-range: Element displaying current decade range (e.g., "2020 - 2029")
  • today: Applied to the current year
  • selected: Applied to selected year(s)
  • flatpickr-yearSelect-theme-light: Applied when using the 'light' theme
  • flatpickr-yearSelect-theme-dark: Applied when using the 'dark' theme

Advanced Usage

Integration with Form Submission

import flatpickr from 'flatpickr';
import yearSelectPlugin from './yearSelectPlugin';

const yearPicker = flatpickr('#year-input', {
  plugins: [yearSelectPlugin()],
  onChange: (selectedDates, dateStr) => {
    console.log('Selected year:', dateStr);
    document.getElementById('hidden-year-value').value = dateStr;
  }
});

// Form submission example
document.getElementById('my-form').addEventListener('submit', (e) => {
  e.preventDefault();
  const selectedYear = document.getElementById('hidden-year-value').value;
  // Process form data
});

Custom Event Handling

import flatpickr from 'flatpickr';
import yearSelectPlugin from './yearSelectPlugin';

const yearPicker = flatpickr('#year-custom', {
  plugins: [yearSelectPlugin()],
  onYearChange: (selectedDates, dateStr) => {
    // Custom logic when year changes
  },
  onClose: (selectedDates, dateStr) => {
    // Handle picker closing
  }
});

API Integration

Programmatically Set Year

import flatpickr from 'flatpickr';
import yearSelectPlugin from './yearSelectPlugin';

const yearPicker = flatpickr('#year-api', {
  plugins: [yearSelectPlugin()]
});

// Set to current year
document.getElementById('set-current-year').addEventListener('click', () => {
  const now = new Date();
  yearPicker.setDate(now);
});

// Set to specific year
document.getElementById('set-specific-year').addEventListener('click', () => {
  yearPicker.setDate('2025');
});

Getting Selected Year

const selectedYear = yearPicker.selectedDates[0].getFullYear();

Browser Compatibility

The plugin is compatible with all browsers supported by flatpickr. It uses modern JavaScript features, so older browsers may require appropriate polyfills.

License

MIT © MikeSha