Skip to main content

tdsEvent.addEventListener

Subscribes to TDS (Toss Design System) events. Currently supports navigationAccessoryEvent — fired when a navigation accessory button (registered with partner.addAccessoryButton) is clicked.

Signature

import { tdsEvent } from '@apps-in-toss/web-framework';

type TdsEvent = {
navigationAccessoryEvent: {
onEvent: (data: { id: string }) => void;
onError?: (error: Error) => void;
options: undefined;
};
};

declare const tdsEvent: {
addEventListener: <K extends keyof TdsEvent>(
event: K,
handlers: {
onEvent: TdsEvent[K]['onEvent'];
onError?: TdsEvent[K]['onError'];
options?: TdsEvent[K]['options'];
}
) => () => void;
};

Parameters

NameTypeRequiredDescription
event'navigationAccessoryEvent'Event name to subscribe.
handlers.onEvent(data: { id: string }) => voidCalled when a button is clicked. data.id is the button ID from partner.addAccessoryButton.
handlers.onError(error: Error) => voidCalled on error.
handlers.optionsundefinedNot used currently.
FieldTypeDescription
idstringThe unique ID of the button registered with partner.addAccessoryButton.

Returns

  • () => void — cleanup function that unsubscribes. Use as the useEffect return value.

Permission

No permission required — tdsEvent.addEventListener is not bound to a PermissionName.

Examples

Minimal — navigationAccessoryEvent

import { tdsEvent } from '@apps-in-toss/web-framework';
import { useEffect } from 'react';

function Page() {
useEffect(() => {
const cleanup = tdsEvent.addEventListener('navigationAccessoryEvent', {
onEvent: ({ id }) => {
console.log('Accessory button clicked:', id);
},
});
return cleanup;
}, []);

return <main>{/* page content */}</main>;
}

Realistic — paired with partner.addAccessoryButton

import { partner, tdsEvent } from '@apps-in-toss/web-framework';
import { useEffect, useState } from 'react';

function ArticlePage() {
const [isLiked, setIsLiked] = useState(false);

useEffect(() => {
// 1. Add a heart button to the top navigation
partner.addAccessoryButton({
id: 'like-button',
title: 'Like',
icon: { name: 'icon-heart-mono' },
});

// 2. Subscribe to clicks
const cleanup = tdsEvent.addEventListener('navigationAccessoryEvent', {
onEvent: ({ id }) => {
if (id === 'like-button') {
setIsLiked((prev) => !prev);
}
},
});

return () => {
cleanup();
// Remove button when the page unmounts
partner.removeAccessoryButton();
};
}, []);

return (
<main>
<p>{isLiked ? '❤️ Liked' : '🤍 Not liked'}</p>
</main>
);
}

Try it live

Subscribe to navigationAccessoryEvent directly on the Events page in sdk-example.

Open in sdk-example

External references