@manufosela/url-params

@manufosela/url-params

URL parameter management utility for getting, setting, and manipulating URL query strings

url-params Demo

Quick usage

import { getParam, setParam } from '@manufosela/url-params';

const page = getParam('page', '1');
setParam('page', Number(page) + 1);

Current URL

const url = new URL(window.location.href);
url.searchParams.set('page', '1');
url.searchParams.set('sort', 'name');
history.replaceState({}, '', url);

Get Parameters

Click a button to get params...
import { getParam, getParams } from '@manufosela/url-params';

getParam('page');
getParams();

Set Parameters

Set a parameter to update the URL...
import { setParam, setParams } from '@manufosela/url-params';

setParam('page', 2);
setParams({ sort: 'name', filter: 'active' });

Remove Parameters

import { removeParam } from '@manufosela/url-params';

removeParam('page');

Parse/Build Query Strings

Parse or build query strings...
import { parseQueryString, toQueryString } from '@manufosela/url-params';

parseQueryString('?tags=a&tags=b');
toQueryString({ tags: ['a', 'b'] });
Demo code (CodePen-ready HTML, CSS, JS)
HTML (html)
<div class="top-links">
    <a href="https://manufosela.dev/utils/">Utils catalog</a>
    <a href="https://manufosela.dev/">Main catalog</a>
    <a href="https://github.com/manufosela/utils/tree/main/packages/url-params">Source</a>
  </div>
        <demo-theme-toggle></demo-theme-toggle>
  <h1>url-params Demo</h1>

  <div class="demo-section">
    <h2>Quick usage</h2>
    <pre class="code-block"><code>import { getParam, setParam } from '@manufosela/url-params';

const page = getParam('page', '1');
setParam('page', Number(page) + 1);
</code></pre>
  </div>

  <div class="demo-section">
    <h2>Current URL</h2>
    <div class="current-url" id="current-url"></div>
    <button id="refresh-url">Refresh Display</button>
    <pre class="code-block"><code>const url = new URL(window.location.href);
url.searchParams.set('page', '1');
url.searchParams.set('sort', 'name');
history.replaceState({}, '', url);
</code></pre>
  </div>

  <div class="demo-section">
    <h2>Get Parameters</h2>
    <div class="row">
      <input type="text" id="get-key" placeholder="Key" value="page">
      <button id="get-param">Get Param</button>
      <button id="get-all">Get All Params</button>
    </div>
    <div class="output" id="get-output">Click a button to get params...</div>
    <pre class="code-block"><code>import { getParam, getParams } from '@manufosela/url-params';

getParam('page');
getParams();
</code></pre>
  </div>

  <div class="demo-section">
    <h2>Set Parameters</h2>
    <div class="row">
      <input type="text" id="set-key" placeholder="Key" value="page">
      <input type="text" id="set-value" placeholder="Value" value="1">
      <button id="set-param">Set Param</button>
    </div>
    <div class="row">
      <button id="set-example">Set Example: page=2&sort=name&filter=active</button>
    </div>
    <div class="output" id="set-output">Set a parameter to update the URL...</div>
    <pre class="code-block"><code>import { setParam, setParams } from '@manufosela/url-params';

setParam('page', 2);
setParams({ sort: 'name', filter: 'active' });
</code></pre>
  </div>

  <div class="demo-section">
    <h2>Remove Parameters</h2>
    <div class="row">
      <input type="text" id="remove-key" placeholder="Key" value="page">
      <button id="remove-param">Remove Param</button>
      <button id="clear-all" class="danger">Clear All</button>
    </div>
    <pre class="code-block"><code>import { removeParam } from '@manufosela/url-params';

removeParam('page');
</code></pre>
  </div>

  <div class="demo-section">
    <h2>Parse/Build Query Strings</h2>
    <div class="row">
      <input type="text" id="parse-input" placeholder="Query string" value="?a=1&b=2&tags=x&tags=y" style="width: 300px;">
      <button id="parse">Parse</button>
    </div>
    <div class="row">
      <button id="build">Build from Object</button>
    </div>
    <div class="output" id="parse-output">Parse or build query strings...</div>
    <pre class="code-block"><code>import { parseQueryString, toQueryString } from '@manufosela/url-params';

parseQueryString('?tags=a&tags=b');
toQueryString({ tags: ['a', 'b'] });
</code></pre>
  </div>
CSS (css)
:root {
  --bg: #0c0f14;
  --bg-elevated: #141923;
  --bg-panel: #171d28;
  --border: #262f3f;
  --text: #f4f6fb;
  --text-muted: #a7b0c2;
  --text-dim: #7d879b;
  --accent: #ff8a3d;
  --accent-strong: #ff6a00;
  --accent-soft: rgba(255, 138, 61, 0.16);
  --shadow: 0 20px 50px rgba(5, 8, 14, 0.45);
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --max-width: 1160px;
}

h1 { color: var(--accent); }
    .demo-section {
      background: var(--bg-elevated);
      padding: 2rem;
      border-radius: 8px;
      margin-bottom: 2rem;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .demo-section h2 {
      margin-top: 0;
      color: var(--text-muted);
    }
    button {
      padding: 0.5rem 1rem;
      margin: 0.25rem;
      border: none;
      border-radius: 4px;
      background: var(--accent);
      color: white;
      cursor: pointer;
    }
    button:hover { background: var(--accent-strong); }
    button.danger { background: #dc3545; }
    button.danger:hover { background: #c82333; }
    input {
      padding: 0.5rem;
      margin: 0.25rem;
      border: 1px solid var(--border);
      border-radius: 4px;
      background: var(--bg-panel);
      color: var(--text);
      width: 150px;
    }
    .output {
      margin-top: 1rem;
      padding: 1rem;
      background: var(--code-bg, #0b0f1a);
      border-radius: 4px;
      font-family: monospace;
      font-size: 0.875rem;
      white-space: pre-wrap;
      word-break: break-all;
      color: var(--code-text, #d6d9e6);
      border: 1px solid var(--border);
    }
    .row {
      display: flex;
      gap: 0.5rem;
      margin-bottom: 0.5rem;
      align-items: center;
      flex-wrap: wrap;
    }
    .top-links {
      display: flex;
      gap: 0.5rem;
      flex-wrap: wrap;
      margin-bottom: 1rem;
    }
    .top-links a {
      font-size: 0.8rem;
      color: var(--text-muted);
      text-decoration: none;
      border: 1px solid var(--border);
      padding: 0.35rem 0.75rem;
      border-radius: 999px;
    }

    .code-block {
      background: #0b0f1a;
      border: 1px solid #2b3247;
      border-radius: 8px;
      padding: 12px;
      color: #d6d9e6;
      font-family: "JetBrains Mono", "Courier New", monospace;
      font-size: 0.85rem;
      white-space: pre-wrap;
      word-break: break-word;
    }
  






:root {
  --bg: #0f1117;
  --bg-2: #151a26;
  --bg-spot-1: #1a2136;
  --bg-spot-2: #1d1b34;
  --card: #1c2233;
  --text: #f3f6ff;
  --muted: #b8c0d9;
  --line: #2b3247;
  --accent: #ffb000;
  --accent-2: #00d0ff;
  --surface: #0b0f1a;
  --code-bg: #0b0f1a;
  --code-text: #d6d9e6;
}

:root.light {
  --bg: #f5f5f7;
  --bg-2: #ffffff;
  --bg-spot-1: #f8e9d0;
  --bg-spot-2: #e8eef8;
  --card: #ffffff;
  --text: #1d1d1f;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #ffb000;
  --accent-2: #00a7d6;
  --surface: #f3f4f6;
  --code-bg: #111827;
  --code-text: #f9fafb;
}

:root.dark {
  --bg: #0f1117;
  --bg-2: #151a26;
  --bg-spot-1: #1a2136;
  --bg-spot-2: #1d1b34;
  --card: #1c2233;
  --text: #f3f6ff;
  --muted: #b8c0d9;
  --line: #2b3247;
  --accent: #ffb000;
  --accent-2: #00d0ff;
  --surface: #0b0f1a;
  --code-bg: #0b0f1a;
  --code-text: #d6d9e6;
}

h1 {
  color: var(--accent) !important;
}

.top-links a,
.topbar a {
  color: var(--muted) !important;
  border-color: var(--line) !important;
}

.top-links a:hover,
.topbar a:hover {
  color: var(--text) !important;
}

.card,
.section,
.demo-section,
.panel {
  background: var(--card) !important;
  border-color: var(--line) !important;
  color: var(--text) !important;
}

.code-block,
pre,
code,
.output,
.current-url,
.event-log,
.result-card,
.log {
  background: var(--code-bg) !important;
  color: var(--code-text) !important;
  border-color: var(--line) !important;
}

input,
select,
textarea {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-color: var(--line) !important;
}

button {
  background: linear-gradient(120deg, var(--accent), #ff6a00) !important;
  color: #111 !important;
}

footer {
  color: var(--muted) !important;
}

footer a {
  color: var(--text) !important;
}
JS (js)
import {
      UrlParams,
      getParam,
      getParams,
      setParam,
      setParams,
      removeParam,
      toQueryString,
      parseQueryString
    } from "https://esm.sh/@manufosela/url-params";

    const currentUrlEl = document.getElementById('current-url');
    const getOutput = document.getElementById('get-output');
    const setOutput = document.getElementById('set-output');
    const parseOutput = document.getElementById('parse-output');

    function ensureDemoParams() {
      const url = new URL(window.location.href);
      if (!url.searchParams.has('demo')) {
        url.searchParams.set('demo', 'true');
        url.searchParams.set('page', '1');
        url.searchParams.set('sort', 'name');
        url.searchParams.set('filter', 'active');
        url.searchParams.set('tags', 'js');
        url.searchParams.append('tags', 'css');
        history.replaceState({}, '', url);
      }
    }

    function updateCurrentUrl() {
      currentUrlEl.textContent = window.location.href;
    }
    currentUrlEl.style.background = 'var(--code-bg, #0b0f1a)';
    currentUrlEl.style.color = 'var(--code-text, #d6d9e6)';
    ensureDemoParams();
    updateCurrentUrl();

    document.getElementById('refresh-url').addEventListener('click', updateCurrentUrl);

    // Get operations
    document.getElementById('get-param').addEventListener('click', () => {
      const key = document.getElementById('get-key').value;
      const value = getParam(key);
      getOutput.textContent = `getParam("${key}") = ${JSON.stringify(value)}`;
    });

    document.getElementById('get-all').addEventListener('click', () => {
      const params = getParams();
      getOutput.textContent = `getParams() = ${JSON.stringify(params, null, 2)}`;
    });

    // Set operations
    document.getElementById('set-param').addEventListener('click', () => {
      const key = document.getElementById('set-key').value;
      const value = document.getElementById('set-value').value;
      setParam(key, value);
      updateCurrentUrl();
      setOutput.textContent = `Set "${key}" = "${value}"`;
    });

    document.getElementById('set-example').addEventListener('click', () => {
      setParams({ page: 2, sort: 'name', filter: 'active' });
      updateCurrentUrl();
      setOutput.textContent = 'Set multiple params: page=2, sort=name, filter=active';
    });

    // Remove operations
    document.getElementById('remove-param').addEventListener('click', () => {
      const key = document.getElementById('remove-key').value;
      removeParam(key);
      updateCurrentUrl();
    });

    document.getElementById('clear-all').addEventListener('click', () => {
      new UrlParams(undefined, { autoUpdate: true }).clear();
      updateCurrentUrl();
    });

    // Parse/build operations
    document.getElementById('parse').addEventListener('click', () => {
      const input = document.getElementById('parse-input').value;
      const result = parseQueryString(input);
      parseOutput.textContent = `parseQueryString("${input}"):\n${JSON.stringify(result, null, 2)}`;
    });

    document.getElementById('build').addEventListener('click', () => {
      const obj = { page: 1, limit: 10, tags: ['js', 'ts', 'css'] };
      const qs = toQueryString(obj);
      parseOutput.textContent = `toQueryString(${JSON.stringify(obj)}):\n${qs}`;
    });
  

class DemoThemeToggle extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
    this.handleClick = this.handleClick.bind(this);
  }

  connectedCallback() {
    const saved = localStorage.getItem('utils-demo-theme');
    document.documentElement.classList.remove('dark');
    document.documentElement.classList.toggle('light', saved === 'light');

    this.render();
  }

  render() {
    const isLight = document.documentElement.classList.contains('light');
    this.shadowRoot.innerHTML = `
      <style>
        :host {
          display: inline-flex;
        }
        .toggle {
          display: inline-flex;
          align-items: center;
          border: 1px solid var(--line);
          border-radius: 999px;
          overflow: hidden;
          background: var(--surface);
        }
        button {
          border: none;
          background: transparent;
          color: var(--muted);
          padding: 6px 12px;
          font-size: 0.8rem;
          cursor: pointer;
          font-family: "Space Grotesk", "Trebuchet MS", Arial, sans-serif;
        }
        button.active {
          background: linear-gradient(120deg, var(--accent), #ff6a00);
          color: #111;
          font-weight: 600;
        }
      </style>
      <div class="toggle" role="group" aria-label="Theme toggle">
        <button class="${isLight ? 'active' : ''}" data-theme="light">Light</button>
        <button class="${isLight ? '' : 'active'}" data-theme="dark">Dark</button>
      </div>
    `;

    this.shadowRoot.querySelectorAll('button').forEach((btn) => {
      btn.addEventListener('click', this.handleClick);
    });
  }

  handleClick(event) {
    const theme = event.currentTarget.dataset.theme;
    const isLight = theme === 'light';
    document.documentElement.classList.toggle('light', isLight);
    document.documentElement.classList.remove('dark');
    localStorage.setItem('utils-demo-theme', isLight ? 'light' : 'dark');
    this.render();
  }
}

customElements.define('demo-theme-toggle', DemoThemeToggle);