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

@shlinkio/shlink-web-component

v0.10.1

Published

Minimal UI to interact with Shlink on React applications

Downloads

633

Readme

Shlink web component

Build Status Code Coverage GitHub release GitHub license Paypal Donate

Minimal UI to interact with Shlink on React applications.

Note This component was originally part of shlink-web-client, and it was extracted so that it could be used in other React-based apps

Installation

npm install @shlinkio/shlink-web-component

Basic usage

This package exports a ShlinkWebComponent react component, which renders a set of sections that can be used to provide a convenient UI to interact with every aspect of a Shlink server: short URLs, tags domains, etc.

The main prop is the apiClient, which is used by the component in order to know how to consume the server's API.

Its implementation is up to the consumer, which gives you the flexibility to directly interact with Shlink from the browser, or through some dedicated proxy or backend-for-frontend.

import { ShlinkWebComponent } from '@shlinkio/shlink-web-component';
import type { ShlinkApiClient } from '@shlinkio/shlink-js-sdk/api-contract';

export function App() {
  const apiClient: ShlinkApiClient = {
    // ...
  };

  return <ShlinkWebComponent apiClient={apiClient} />;
};

Note The API client has to fulfil the type defined in @shlinkio/shlink-js-sdk, but that package is an optional peer dependency. If you don't need to import from it to define your own implementation or to use the implementation provided there, feel free to skip it.

Also, this component re-exports the SDK types for your convenience, so you can choose to import from @shlinkio/shlink-js-sdk/api-contract or @shlinkio/shlink-web-component/api-contract.

Settings

It is possible to customize some aspects of the UI by providing some optional settings.

Settings can control default ordering for lists, real-time updates behavior, optional fields for short URL creation, etc.

Everything related with settings is exposed via the @shlinkio/shlink-web-component/settings entry point.

import { ShlinkWebComponent } from '@shlinkio/shlink-web-component';
import type { Settings } from '@shlinkio/shlink-web-component/settings';

export function App() {
  const settings: Settings = {};

  return (
    <ShlinkWebComponent settings={settings} {...} />
  );
};

This library provides another react component which renders a form designed to configure settings for <ShlinkWebComponent />.

import type { Settings } from '@shlinkio/shlink-web-component/settings';
import { ShlinkWebSettings } from '@shlinkio/shlink-web-component/settings';

export function App() {
  const [settings, setSettings] = useState<Settings>({});

  return (
    <ShlinkWebSettings settings={settings} updateSettings={setSettings} {...} />
  );
};

Routes prefix

Sections are handled via client-side routing with react-router-dom. ShlinkWebComponent will add its own <BrowserRouter /> unless already rendered inside a router.

If you render it inside a router, make sure you pass the prefix for all routes handled by this component.

import { ShlinkWebComponent } from '@shlinkio/shlink-web-component';
import { BrowserRouter, Routes, Route } from 'react-router-dom';

export function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/foo" element={<Foo />} />
        <Route path="/bar" element={<Bar />} />
        <Route path="/shlink-ui" element={<ShlinkWebComponent routesPrefix="/shlink-ui" {...} />} />
      </Routes>
    </BrowserRouter>
  );
};

Tag colors storage

One of the features on ShlinkWebComponent is generating and handling colors for short URL tags.

By default, all colors will be generated from scratch every time the component is rendered, but you can persist them between executions by providing an object implementing this type:

type TagColorsStorage = {
  getTagColors(): Record<string, string>;

  storeTagColors(colors: Record<string, string>): void;
};

For example, imagine you want to persist tag colors in the browser's local storage. You could do something like this:

import { ShlinkWebComponent } from '@shlinkio/shlink-web-component';
import type { TagColorsStorage } from '@shlinkio/shlink-web-component';

class TagColorsLocalStorage implements TagColorsStorage {
  constructor(private readonly localStorage: Storage) {
  }

  getTagColors(): Record<string, string> {
    const colors = this.localStorage.getItem('shlink_tag_colors');
    return colors ? JSON.parse(colors) : {};
  }

  storeTagColors(colors: Record<string, string>): void {
    this.localStorage.setItem('shlink_tag_colors', JSON.stringify(colors));
  }
}

export function App() {
  const tagColorsStorage = new TagColorsLocalStorage(window.localStorage);

  return (
    <ShlinkWebComponent
      tagColorsStorage={tagColorsStorage}
      {...}
    />
  );
};

Styles

Currently, this component depends on bootstrap, @shlinkio/shlink-frontend-kit and its own stylesheets for proper styling.

Make sure you import stylesheets in the order documented here for everything to work.

// src/index.scss
@import 'node_modules/@shlinkio/shlink-frontend-kit/dist/base'; // Before bootstrap stylesheet. Includes SASS var overrides
@import 'node_modules/bootstrap/scss/bootstrap.scss';
@import 'node_modules/@shlinkio/shlink-frontend-kit/dist/index'; // After bootstrap. Includes CSS overrides
@import 'node_modules/@shlinkio/shlink-web-component/dist/index';
import type { Theme } from '@shlinkio/shlink-frontend-kit';
import { Checkbox, changeThemeInMarkup } from '@shlinkio/shlink-frontend-kit';
import { ShlinkWebComponent } from '@shlinkio/shlink-web-component';
import { useCallback, useState } from 'react';
import './src/index.scss'; // The stylesheet above

export function App() {
  const [theme, setTheme] = useState<Theme>('light');
  const toggleTheme = useCallback((isDarkTheme: boolean) => {
    const newTheme: Theme = isDarkTheme ? 'dark' : 'light';

    setTheme(newTheme);
    changeThemeInMarkup(newTheme);
  }, []);

  return (
    <>
      <Checkbox checked={theme === 'dark'} onChange={toggleTheme}>
        Dark theme
      </Checkbox>
      <ShlinkWebComponent {...} />
    </>
  );
};

Dev sandbox

Since this is a complex component, this project provides a convenient way to do some manual tests.

Simply run npm run dev or docker compose up, and a local vite server will be started on port 3002.

Then visit http://localhost:3002 to see your changes in real time.