1.0.1 • Published 5 months ago

activity-grid v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Activity Grid

A customizable activity grid component that creates GitHub-style contribution graphs. This web component allows you to visualize activity data over time with support for various themes, configurations, and interaction options.

npm version License: MIT

Default View

Live Examples

Interactive examples are available on StackBlitz:

React Vue.js Angular TypeScript

Features

  • 📊 GitHub-style activity visualization
  • 🎨 Multiple built-in color themes (green, blue, red, yellow, purple)
  • 🌓 Dark mode support
  • 📅 Flexible date range configuration
  • 🗓️ Customizable week display (start on Monday, skip weekends)
  • 🎯 Interactive cells with click events
  • 💪 TypeScript support
  • 🔌 Framework agnostic - works with any frontend framework

Installation

npm install activity-grid

Basic Usage

<!-- In your HTML -->
<activity-grid id="myGrid"></activity-grid>

<script>
  // Initialize with data
  const grid = document.getElementById('myGrid');
  grid.data = [
    { date: '2024-01-01', count: 5 },
    { date: '2024-01-02', count: 2 },
    // ... more data
  ];
</script>
// If using TypeScript/ES modules
import 'activity-grid';

Data Format

The component accepts an array of activity data points with the following structure:

interface ActivityData {
  /** Date in YYYY-MM-DD format */
  date: string;
  /** Number of activities for this date (must be non-negative) */
  count: number;
  /** Optional identifier for the cell */
  id?: string;
}

Configuration Options

The component can be configured with the following attributes/properties:

PropertyTypeDefaultOptionsDescription
dataActivityData[][]-Array of activity data points
colorThemestring'green''green', 'red', 'blue', 'yellow', 'purple'Predefined color theme for the grid
colorsstring[]['#ebedf0', '#9be9a8', '#40c463', '#30a14e', '#216e39']Any array of 5 valid CSS colorsCustom color array for activity levels
emptyColorstringLight mode: '#ebedf0'Dark mode: '#161b22'Any valid CSS colorColor for days with no activity
darkModebooleanfalsetrue, falseWhether to use dark mode colors
skipWeekendsbooleanfalsetrue, falseWhether to exclude weekends from the grid
startWeekOnMondaybooleanfalsetrue, falseWhether to start weeks on Monday instead of Sunday
startDatestringOne year before end dateAny valid date in YYYY-MM-DD formatStart date for the activity grid
endDatestringCurrent dateAny valid date in YYYY-MM-DD formatEnd date for the activity grid

ActivityData Interface

interface ActivityData {
  /** Date in YYYY-MM-DD format */
  date: string;
  /** Number of activities for this date (must be non-negative) */
  count: number;
  /** Optional identifier for the cell */
  id?: string;
}

Examples

Color Themes

The activity grid comes with several built-in themes:

Default (Green)

<activity-grid></activity-grid>

Default Theme

Blue Theme

<activity-grid color-theme="blue"></activity-grid>

Blue Theme

Red Theme

<activity-grid color-theme="red"></activity-grid>

Red Theme

Yellow Theme

<activity-grid color-theme="yellow"></activity-grid>

Yellow Theme

Purple Theme

<activity-grid color-theme="purple"></activity-grid>

Purple Theme

Custom Colors

<activity-grid colors='["#ebedf0", "#9be9a8", "#40c463", "#30a14e", "#216e39"]'></activity-grid>

Custom Colors

Week Configuration

<!-- Start week on Monday -->
<activity-grid start-week-on-monday></activity-grid>

<!-- Skip weekends -->
<activity-grid skip-weekends></activity-grid>

Dark Mode

<activity-grid dark-mode></activity-grid>

Dark Mode

Custom Date Range

<activity-grid 
  start-date="2024-01-01" 
  end-date="2024-12-31">
</activity-grid>

Custom Date Range

Handling Click Events

const grid = document.querySelector('activity-grid');
grid.addEventListener('cell-click', (event) => {
  const { date, count, id } = event.detail;
  console.log(`Cell clicked: ${date} with ${count} activities`);
});

Framework Integration

React

import 'activity-grid';
import { useEffect, useRef } from 'react';

declare global {
  namespace JSX {
    interface IntrinsicElements {
      'activity-grid': any;
    }
  }
}

function ActivityGridComponent() {
  const gridRef = useRef(null);

  useEffect(() => {
    if (gridRef.current) {
      gridRef.current.data = [
        { date: '2024-01-01', count: 5 },
        // ... more data
      ];
    }
  }, []);

  return <activity-grid ref={gridRef} dark-mode></activity-grid>;
}

Vue

<template>
  <activity-grid ref="grid" dark-mode></activity-grid>
</template>

<script setup lang="ts">
import 'activity-grid';
import { onMounted, ref } from 'vue';

const grid = ref();

onMounted(() => {
  grid.value.data = [
    { date: '2024-01-01', count: 5 },
    // ... more data
  ];
});
</script>

More framework examples coming soon!

Browser Support

The component works in all modern browsers that support Web Components:

  • Chrome/Edge (Chromium-based)
  • Firefox
  • Safari

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.