GitHunt
WA

Waterloo/Tacklet

Tacklet SDK

The official SDK for building custom Tacklets for the Tackpad free-form canvas. The SDK allows developers to create Tacklets that seamlessly integrate with Tackpad.

๐Ÿ“ฆ Installation

npm install tacklet

Or with Yarn:

yarn add tacklet

โšก Quick Start

Your Tacklet will be loaded in an iframe by the Tackpad application. The SDK handles communication between your Tacklet and the parent Tackpad canvas.

Here's a simple example:

import tacklet from 'tacklet';

// Connect to the Tackpad platform
async function initialize() {
  try {
    await tacklet.connect();
    console.log('Connected to Tackpad!');
    document.getElementById('tacklet-status').textContent = 'Connected';

    // Apply theme
    const theme = tacklet.getTheme();
    document.body.classList.add(`theme-${theme}`);

    // Listen for selection events
    tacklet.on('selected', () => {
      console.log('Tacklet selected!');
      document.body.classList.add('tacklet-selected');
    });

    tacklet.on('deselected', () => {
      console.log('Tacklet deselected!');
      document.body.classList.remove('tacklet-selected');
    });
  } catch (err) {
    console.error('Failed to connect to Tackpad:', err);
  }
}

initialize();

๐Ÿ”„ Tacklet Lifecycle

  1. Tacklet is loaded in an iframe by Tackpad.
  2. Call tacklet.connect() to initialize communication.
  3. Use the SDK's methods to interact with Tackpad.
  4. Save Tacklet state using setData() and retrieve it with getData().
  5. Call destroy() when your Tacklet is unmounted.

๐Ÿ“Š Data Persistence

The SDK provides methods to save and retrieve Tacklet-specific data:

// Save Tacklet data
async function saveCounter() {
  const currentCount = parseInt(document.getElementById('counter').textContent, 10);
  await tacklet.setData({ count: currentCount });
  console.log('Counter saved!');
}

// Load Tacklet data
async function loadCounter() {
  try {
    const data = await tacklet.getData();
    if (data?.count !== undefined) {
      document.getElementById('counter').textContent = data.count;
    }
  } catch (err) {
    console.error('Failed to load counter:', err);
  }
}

๐ŸŽจ Theming

Ensure theme consistency with the Tackpad environment:

async function applyTheme() {
  await tacklet.connect();
  const theme = tacklet.getTheme(); // 'light' or 'dark'
  document.body.classList.add(`theme-${theme}`);
}

applyTheme();

๐Ÿ“ Dimensions and Layout

Get Tacklet dimensions and listen for size changes:

const { width, height } = tacklet.getDimensions();
console.log(`Tacklet dimensions: ${width}x${height}`);

tacklet.on('resized', ({ width, height }) => {
  console.log(`Tacklet resized to: ${width}x${height}`);
  updateLayout(width, height);
});

๐Ÿ“– API Reference

The Tacklet SDK is a singleton and should not be instantiated manually.

Methods

Method Description
connect() Connects to the Tackpad platform. Must be called before using other methods.
setData(data) Saves widget data.
getData() Retrieves widget data.
on(eventType, handler) Registers an event listener.
off(eventType, handler) Removes an event listener.
getNodeId() Returns the Tacklet's unique node ID.
getTheme() Returns the current theme (light or dark).
getDimensions() Returns the Tacklet's dimensions.
getIsSelected() Returns whether the Tacklet is currently selected.
destroy() Cleans up the SDK connection. Call when Tacklet is unmounted.
setDebug(state) Enables debug logging. Example: tacklet.setDebug(true);

Events

Event Description
ready Fired when the SDK is connected and ready.
selected Fired when the Tacklet is selected.
deselected Fired when the Tacklet is deselected.
resized Fired when the Tacklet is resized.
moved Fired when the Tacklet is moved.
data Fired when Tacklet data is saved.

๐Ÿ“ฑ Framework Integration Examples

The SDK can be used in different frameworks:

React Integration

import React, { useState, useEffect } from 'react';
import tacklet from 'tacklet';

function CounterWidget() {
  const [count, setCount] = useState(0);
  const [isSelected, setIsSelected] = useState(false);
  const [theme, setTheme] = useState('light');

  useEffect(() => {
    async function initialize() {
      try {
        await tacklet.connect();
        console.log('Connected to Tackpad!');
        setTheme(tacklet.getTheme());

        // Load saved data
        const data = await tacklet.getData();
        if (data?.count !== undefined) setCount(data.count);

        // Event listeners
        const handleSelected = () => setIsSelected(true);
        const handleDeselected = () => setIsSelected(false);

        tacklet.on('selected', handleSelected);
        tacklet.on('deselected', handleDeselected);

        return () => {
          tacklet.off('selected', handleSelected);
          tacklet.off('deselected', handleDeselected);
          tacklet.destroy();
        };
      } catch (err) {
        console.error('Failed to connect to Tackpad:', err);
      }
    }

    initialize();
  }, []);

  useEffect(() => {
    tacklet.setData({ count }).catch(err => console.error('Failed to save data:', err));
  }, [count]);

  return (
    <div className={`widget ${theme} ${isSelected ? 'selected' : ''}`}>
      <div>{count}</div>
      <button onClick={() => setCount(count + 1)}>+</button>
      <button onClick={() => setCount(count - 1)}>-</button>
      <button onClick={() => setCount(0)}>Reset</button>
    </div>
  );
}

export default CounterWidget;

๐Ÿงฉ Development

To build your own Tacklet:

  1. Create your Tacklet using any framework or vanilla JavaScript.
  2. Use the Tacklet SDK to communicate with Tackpad.
  3. Host your Tacklet on a web server.
  4. Register your Tacklet with Tackpad to make it available to users.

For detailed development instructions, see the Developer Guide.

๐Ÿ“„ License

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

Waterloo/Tacklet | GitHunt