Minimal Access
A copy-paste React access screen for internal tools and quick gates — a single masked field, no frills. One file, zero dependencies.
Best used for A quick, no-frills gate for an internal tool where tone doesn't matter.
For when a segmented code doesn't fit the tone of what you're gating — one field, one button, done. No footer text unless you pass your own.
Minimal setup
<MinimalAccessTemplate expectedHash={process.env.NEXT_PUBLIC_KNOCK_HASH}>
<YourApp />
</MinimalAccessTemplate>
- components
- knock-codes
- core
- react
// Minimal Access Template v1.0.0
"use client";
import { useEffect, useState, type ReactNode } from "react";
import { useKnockCodes } from "./useKnockCodes.ts";
import { PinInput } from "./PinInput.tsx";
import { DEFAULT_LABELS, type KnockCodesConfig, type KnockCodesLabels } from "./types.ts";
import { cx } from "./cx.ts";
// Shakes the card on a failed attempt — inlined via a plain `<style>` tag
// (not a Tailwind config keyframe) so this file works standalone in a host
// project that has no matching keyframe of its own.
// Wrapped in the reduced-motion query rather than toggled from JS: under
// `prefers-reduced-motion: reduce` this keyframe name simply doesn't exist,
// so the `animation: minimal-access-shake …` utility below resolves to no
// visual effect.
const MINIMAL_ACCESS_SHAKE_KEYFRAMES = `@media (prefers-reduced-motion: no-preference) {
@keyframes minimal-access-shake {
10%, 90% { transform: translateX(-1px); }
20%, 80% { transform: translateX(2px); }
30%, 50%, 70% { transform: translateX(-4px); }
40%, 60% { transform: translateX(4px); }
}
}`;
export interface MinimalAccessTemplateLabels extends KnockCodesLabels {
description?: string;
supportLabel?: string;
footerText?: ReactNode;
}
export interface MinimalAccessTemplateProps extends KnockCodesConfig {
children: ReactNode;
/** Rendered above the heading — your own logo/wordmark. Omitted entirely if not passed. */
logo?: ReactNode;
labels?: MinimalAccessTemplateLabels;
/** Renders "Contact support" as a link. Ignored if `onContactSupport` is also set. */
supportHref?: string;
/** Renders "Contact support" as a button instead of a link. */
onContactSupport?: () => void;
/** Set false to embed this somewhere other than a real page root. @default true */
fullPage?: boolean;
/** Forces light or dark presentation, independent of any ancestor ".dark" class. Omit to follow one if it exists. */
theme?: "light" | "dark";
/**
* Persist the unlocked session across reloads within the same tab via
* sessionStorage. Not a security boundary — clearable from DevTools or a
* private window, same as any other client-side storage. @default undefined (off)
*/
remember?: "session";
autoFocus?: boolean;
className?: string;
}
const TEMPLATE_LABELS: Required<MinimalAccessTemplateLabels> = {
...DEFAULT_LABELS,
heading: "Enter access code",
description: "This page is private.",
supportLabel: "Contact support",
footerText: "",
};
/**
* The leanest possible access screen — a single masked field, a
* small plain card, no segmented boxes and no default footer copy. Same
* `useKnockCodes` contract as every other block, just the smallest possible
* presentation. For segmented code entry, use `<KnockCodesTemplate>` instead.
*/
export function MinimalAccessTemplate({
children,
logo,
labels,
supportHref,
onContactSupport,
fullPage = true,
theme,
remember,
autoFocus = true,
className,
...config
}: MinimalAccessTemplateProps) {
const merged = { ...TEMPLATE_LABELS, ...labels };
const { state, error, submit, ready } = useKnockCodes({
...config,
storage: remember === "session" ? "sessionStorage" : config.storage,
});
const [code, setCode] = useState("");
const [shakeSeed, setShakeSeed] = useState(0);
const [showChildren, setShowChildren] = useState(false);
useEffect(() => {
if (error) setShakeSeed((seed) => seed + 1);
}, [error]);
// Holds the unlock screen visible for a beat so success has a visible
// transition instead of an instant swap to `children`.
useEffect(() => {
if (state !== "unlocked") {
setShowChildren(false);
return;
}
const timer = setTimeout(() => setShowChildren(true), 550);
return () => clearTimeout(timer);
}, [state]);
function SuccessPanel({ theme }: { theme?: "light" | "dark" }) {
const panel = (
<div className="flex h-full min-h-[26rem] w-full items-center justify-center bg-[var(--ag-canvas-bg,#f9fafb)] p-6 dark:bg-[var(--ag-canvas-bg-dark,#0b1220)]">
<div className="flex flex-col items-center gap-2 text-center">
<div className="flex h-10 w-10 items-center justify-center rounded-full bg-green-100 text-green-600 dark:bg-green-500/10 dark:text-green-400">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} className="h-5 w-5">
<path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
</svg>
</div>
<p className="text-sm font-medium text-gray-700 dark:text-gray-200">Access granted</p>
</div>
</div>
);
return theme === "dark" ? <div className="dark h-full w-full">{panel}</div> : panel;
}
if (!ready) return null;
if (state === "unlocked") {
if (!showChildren) {
return <SuccessPanel theme={theme} />;
}
return <>{children}</>;
}
const handleSubmit = async () => {
if (!code || state === "submitting") return;
await submit(code);
setCode("");
};
const content = (
<div
className={cx(
"flex w-full items-center justify-center bg-[var(--ag-canvas-bg,#f9fafb)] p-6 dark:bg-[var(--ag-canvas-bg-dark,#0b1220)]",
fullPage ? "min-h-[100dvh]" : "h-full",
className
)}
>
<div
key={shakeSeed}
style={{ fontFamily: "var(--ag-font, inherit)" }}
className={cx(
"w-full max-w-sm rounded-[var(--ag-radius,0.75rem)] border border-[var(--ag-border,#e5e7eb)] bg-[var(--ag-card,#ffffff)] p-7 dark:border-[var(--ag-border-dark,#1f2937)] dark:bg-[var(--ag-card-dark,#030712)]",
shakeSeed > 0 && "animate-[minimal-access-shake_0.4s_ease-in-out]"
)}
>
<style>{MINIMAL_ACCESS_SHAKE_KEYFRAMES}</style>
{logo && <div className="mb-5">{logo}</div>}
<h1 className="text-lg font-semibold text-gray-900 dark:text-gray-50">{merged.heading}</h1>
{merged.description && <p className="mt-1 text-sm text-gray-500 dark:text-gray-400">{merged.description}</p>}
<div className="mt-5">
<PinInput
value={code}
onChange={setCode}
onSubmit={() => void handleSubmit()}
submitting={state === "submitting"}
error={error}
labels={labels}
autoFocus={autoFocus}
/>
</div>
{(supportHref || onContactSupport) && (
<div className="mt-3 text-center">
{onContactSupport ? (
<button type="button" onClick={onContactSupport} className="text-xs font-medium text-gray-500 hover:underline dark:text-gray-400">
{merged.supportLabel}
</button>
) : (
<a href={supportHref} className="text-xs font-medium text-gray-500 hover:underline dark:text-gray-400">
{merged.supportLabel}
</a>
)}
</div>
)}
{merged.footerText && <p className="mt-4 text-center text-xs text-gray-400 dark:text-gray-500">{merged.footerText}</p>}
</div>
</div>
);
return theme === "dark" ? <div className="dark h-full w-full">{content}</div> : content;
}
Add this template to your project
Recommended
npx shadcn@latest add @knock-codes/minimal-access-templateAlso installs
- Knock Codes Core
- Knock Codes Types
- useKnockCodes
- cx (classname helper)
- PIN Input
These install together as one atomic unit — even a presentational or read-only piece needs the full verification stack (hook, types, core) behind it to actually run.
Files created (10)
- 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/KnockCodesContext.tsx
- components/knock-codes/react/cx.ts
- components/knock-codes/react/PinInput.tsx
- components/knock-codes/react/MinimalAccessTemplate.tsx
Other ways
GitHub shorthand
npx shadcn@latest add trivedi-vatsal/knock-codes/minimal-access-templateCopy 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.
Installing via an AI agent?
Drop AGENTS.md into your project root — it instructs any coding agent to hash the code locally, write only the hash, and confirm the plaintext never touched a file. Thin pointer files exist for tools that read a different filename.
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| expectedHash | string | — | SHA-256 hex hash to verify against, for local mode. |
| verify | VerifyFn | — | Custom async verification function, for server mode. |
| validateSession | (session: KnockCodesSession) => boolean | Promise<boolean> | — | Called when a session is read from storage. Return false or throw to reject it (server-mode token check). |
| children * | ReactNode | — | Rendered once unlocked. |
| logo | ReactNode | — | Rendered above the heading — your own logo/wordmark. Omitted entirely if not passed. |
| supportHref | string | — | Renders "Contact support" as a link to this URL. |
| onContactSupport | () => void | — | Renders "Contact support" as a button instead of a link — e.g. to open a chat widget. |
| labels | MinimalAccessTemplateLabels | — | Overrides heading, description, input label, support label, footer text, and every KnockCodesLabels string. |
| fullPage | boolean | true | Set false to embed this somewhere other than a real page root (a demo, a docs preview) — drops the full-viewport (100dvh) sizing. |
| theme | "light" | "dark" | — | Forces light or dark presentation on its own, independent of any ancestor ".dark" class. Omit to follow the nearest ".dark" ancestor if one happens to exist. |
| remember | "session" | — | Persists the unlocked session across reloads within the same tab via sessionStorage. Not a security boundary — see the security model. |
| className | string | — | Extra classes on the full-page backdrop. |
Exactly one of expectedHash or verify is required.
Accessibility
The code field has its own label and an accessible show/hide toggle (aria-label switches between "Show code"/"Hide code"). Errors and the submitting state announce through a shared aria-live="polite" region, same contract as every other block.
Customization
Everything is one file — background, card, heading, description, field, button, support link, and footer are all inline, not composed from Gate Wrapper/PIN Input. Pass `logo` for your own wordmark and `labels` to override every string. For segmented code entry instead of a single field, use Knock Codes Template instead.
Need a hash? Use the hash generator on Getting Started — computed locally, never sent anywhere.
The honest version
Knock Codes stops casual visitors, search engines, and forwarded links. Local mode does not stop anyone who opens DevTools — the hash ships in your client bundle by design. Server mode (swap one prop) hides the hash from the client; children you already bundled are still in the JavaScript, and a forged session works unless you wire validateSession. A velvet rope, with an optional real lock. Never marketed as more than that.
Blocks this template is built from
Drop down to these directly once you need more control than the single-file template gives you.
- Knock CodesA copy-paste React access screen for gating a whole page or app root — local hash or server verification, one prop swap between them. Zero dependencies.
- PIN InputA copy-paste React access-code field for building a custom gate on useKnockCodes — masked field or segmented boxes, paste support, accessible errors. Zero dependencies.
- Standalone GateA copy-paste React access screen for the fastest possible integration — wrap your app, pass a hash, done. Zero dependencies.