Core

Knock Codes

The main gate — renders children when unlocked, otherwise an access-code prompt.

coregate

Best used for The default choice for gating a whole page or app root in local mode.

The component every other block in this library either wraps or composes on. Local SHA-256 verification by default; swap expectedHash for verify to upgrade to server-mode protection with no other code changes.

Loading preview…
Installation

Add this block to your project

CLI

npx shadcn@latest add http://localhost:3000/r/react/knock-codes.json

Running 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
  • cx (classname helper)
  • Gate Wrapper
  • PIN Input

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/cx.ts
  • components/knock-codes/react/GateWrapper.tsx
  • components/knock-codes/react/PinInput.tsx
  • components/knock-codes/react/KnockCodes.tsx

No CLI? Copy the files by hand

  1. Open the Code tab in the preview above.
  2. Create each path listed below in your project and paste its contents in.
  3. Do the same for anything listed under “Also installs”, if present.

Props

API reference

PropTypeDefaultDescription
expectedHashstringSHA-256 hex hash to verify against, for local mode. Mutually exclusive with verify.
verifyVerifyFnCustom async verification function, for server mode. Mutually exclusive with expectedHash.
storage"localStorage" | "sessionStorage" | "memory""localStorage"Where the unlocked session persists.
storageKeystring"knock-codes:session"Storage key the session is written under.
timeoutnumber1800000Session lifetime in milliseconds (30 minutes).
activityTrackingbooleanfalseSliding-timeout model — interaction rewrites expiry instead of a fixed TTL.
children *ReactNodeRendered once unlocked.
labelsKnockCodesLabelsOverrides for every user-facing string.
variant"page" | "inline""page"Outer positioning — full-page centered, or flows inline in an existing layout.
classNamestringExtra classes on the outer wrapper.
Notes

Accessibility

The access-code field is a labeled, masked text input with native paste support. Errors and the submitting state announce through an aria-live="polite" status region, so screen reader users hear "Checking..." and any error without focus moving. The whole flow works keyboard-only: tab to the field, type, Enter to submit.

Customization

Every string is overridable through the `labels` prop for localization. The wrapper has no fixed visual opinions beyond the default card — pass `className` to restyle the shell, or skip this component and call `useKnockCodes` directly to build a fully custom prompt.

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.