→ Session
Logout Button
Reads the shared session from a Session Provider and clears it on click.
Best used for Any screen sharing a Session Provider session that needs a manual way to lock again.
Requires a <SessionProvider> ancestor — a logout action only makes sense where a session is already being
shared with something else that reads it.
- components
- knock-codes
- core
- react
"use client";
import type { ReactNode } from "react";
import { useKnockCodesContext } from "./KnockCodesProvider.tsx";
import { cx } from "./cx.ts";
export interface LogoutButtonProps {
children?: ReactNode;
className?: string;
/** Called after the shared session is cleared. */
onLoggedOut?: () => void;
}
/**
* Reads the shared session from `<KnockCodesProvider>` and clears it on
* click. Requires a provider — a logout action only makes sense where a
* session is already being shared with something else that reads it.
*/
export function LogoutButton({ children = "Log out", className, onLoggedOut }: LogoutButtonProps) {
const { logout } = useKnockCodesContext();
return (
<button
type="button"
onClick={() => {
logout();
onLoggedOut?.();
}}
className={cx(
"rounded-[var(--ag-radius,0.375rem)] border border-[var(--ag-border,#d9d2c2)] px-3 py-1.5 text-sm text-[#191a18] hover:border-[var(--ag-primary,#187c74)]/40 hover:bg-[var(--ag-primary,#187c74)]/5 dark:border-[var(--ag-border-dark,#26302b)] dark:text-[#edeae0] dark:hover:border-[var(--ag-primary-dark,#4fd1c5)]/40 dark:hover:bg-[var(--ag-primary-dark,#4fd1c5)]/10",
className
)}
>
{children}
</button>
);
}
Add this block to your project
CLI
npx shadcn@latest add http://localhost:3000/r/react/logout-button.jsonRunning this site somewhere other than localhost? Set NEXT_PUBLIC_SITE_URL and this command switches to that host automatically.
Also installs
- Knock Codes Core
- Knock Codes Types
- useKnockCodes
- Session Provider
- cx (classname helper)
Files created (9)
- components/knock-codes/core/hash.ts
- components/knock-codes/core/verify.ts
- components/knock-codes/core/session.ts
- components/knock-codes/core/storage.ts
- components/knock-codes/react/types.ts
- components/knock-codes/react/useKnockCodes.ts
- components/knock-codes/react/KnockCodesProvider.tsx
- components/knock-codes/react/cx.ts
- components/knock-codes/react/LogoutButton.tsx
No CLI? Copy the files by hand
- Open the Code tab in the preview above.
- Create each path listed below in your project and paste its contents in.
- Do the same for anything listed under “Also installs”, if present.
→ Props
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | "Log out" | Button label. |
| className | string | — | Extra classes on the button. |
| onLoggedOut | () => void | — | Called after the shared session is cleared. |
Accessibility
A real <button>, not a link or div — full keyboard operability and screen-reader semantics for free.
Customization
Pass children to change the label, or `onLoggedOut` to run something after the session clears (redirect, analytics, whatever your app needs).
→ Compose with
Blocks that pair well with this one
These combine naturally with this block, whether as a shared shell, a shared session, or a common fallback.
- Session ProviderShares one useKnockCodes session across a tree, so multiple components stay in sync.
- Session Timeout BannerWarns before the shared session expires, with a one-click way to log out immediately.
- Access ReceiptA small session audit strip shown after unlock — timestamp, storage mode, timeout, and verification strategy.