Clerk JavaScript SDKs

repository·main·Indexed 23 days ago

https://github.com/clerk/javascript

Central hub for official Clerk JavaScript SDKs under the @clerk namespace, providing streamlined user management for sign-up, sign-in, and profile management experiences. This repository includes comprehensive guides for running integration tests using Playwright, configuring self-signed SSL certificates with mkcert, and managing test environments across local and staging setups.

Tokens
175.4K
Snippets
378
Records
1K
Agent score
82%

What's inside @clerk/javascript

  1. Overview of @clerk/headless

    main

    The @clerk/headless package provides unstyled, accessible React UI primitives. These components handle complex logic such as positioning, keyboard navigation, focus management, and ARIA attributes using Floating UI.

    Note: This package is marked as internal (private: true) and is intended to be consumed by @clerk/ui. It is separated from @clerk/ui to avoid JSX transform conflicts between the standard react-jsx required by these primitives and the @emotion/react source used by @clerk/ui.

  2. Use @clerk/electron-passkeys for native WebAuthn support

    main

    The @clerk/electron-passkeys package provides native passkey (WebAuthn) support for @clerk/electron. It is specifically designed for Electron windows loading local bundles (via file:// or custom protocols) where the renderer's built-in WebAuthn fails origin checks.

    Important: This package is a napi-rs native module that should be loaded in the Electron main process. You typically do not call it directly; instead, you enable it via @clerk/electron using createClerkBridge({ passkeys: true }).

    Platform Support

    PlatformBackendAuthenticators
    macOS 12+AuthenticationServices (ASAuthorizationController)Touch ID, iCloud Keychain, security keys
    Windows 10 1903+webauthn.dll (Windows WebAuthn API)Windows Hello, security keys
    LinuxNot supported (use renderer WebAuthn)
    WARNING

    This package is under active development and is not yet ready for production use.

  3. Use the Organization Profile Domains Section

    main

    The Organization Profile Domains Section is a high-level component used to manage an organization's domains. It provides a unified interface for:

    • Listing existing domains.
    • Adding and verifying new domains via an add/verify wizard.
    • Editing the enrollment mode of verified domains.
    • Removing domains via a removal confirmation flow.

    The component manages its own internal state using three distinct state machines to coordinate the list view, the add/verify wizard, the enrollment editor, and the removal confirmation flows.

  4. Use @clerk/localizations to customize Clerk component strings

    main

    The @clerk/localizations package provides localized strings for Clerk components. You can use these strings to provide localization for your users or to override default wording to match your brand's voice.

    Note: Clerk officially maintains only the en-US (English - United States) locale. All other language translations in this package are community contributions.

  5. Use the FileUpload headless primitive

    main

    The FileUpload primitive is a headless component for managing file uploads. It handles the internal <input type="file">, drag-and-drop logic, and file validation (accept and maxSize), while emitting zero styles. You control the UI using provided parts and data-* attributes.

    Key features:

    • Trigger & Dropzone: Users can pick files via a button or by dragging them onto a zone.
    • File Items: Selected files are rendered as Item components with optional ItemPreview (thumbnails for images).
    • Modes: Supports single-file mode (e.g., for avatars) where new selections replace old ones, and multiple-file mode where selections are appended.
    • Validation: Automatically rejects files based on accept and maxSize, reporting them via onReject.
    import { FileUpload } from '@clerk/headless/file-upload';
    
    function Uploader() {
      return (
        <FileUpload.Root
          accept='image/*'
          multiple
        >
          <FileUpload.Dropzone>
            <FileUpload.Trigger>Choose files</FileUpload.Trigger>
            <p>or drag files here</p>
          </FileUpload.Dropzone>
    
          <FileList />
        </FileUpload.Root>
      );
    }
    
    function FileList() {
      const { files } = FileUpload.useFileUpload();
      return (
        <ul>
          {files.map(file => (
            <li key={file.name}>
              <FileUpload.Item file={file}>
                <FileUpload.ItemPreview />
                <span>{file.name}</span>
                <FileUpload.ItemDelete>Remove</FileUpload.ItemDelete>
              </FileUpload.Item>
            </li>
          ))}
        </ul>
      );
    }
  6. What is the Menu primitive in @clerk/headless?

    main

    The Menu is a headless primitive from @clerk/headless that provides a floating list of actions triggered by a button. It manages complex logic including:

    • Open/closed state
    • Portalling to avoid overflow issues
    • Floating-UI positioning
    • Roving-focus keyboard navigation (including typeahead)
    • Submenu coordination
    • ARIA wiring

    Because it is headless, it ships no styles. You must provide your own CSS by targeting the data-* attributes emitted by each part.

  7. What is the Popover primitive?

    main

    The Popover is a headless primitive from @clerk/headless. It provides the logic for a floating panel anchored to a trigger, including:

    • Open state management
    • Portalling
    • Floating-UI positioning (flip/shift/arrow)
    • Focus management
    • Dismissal (via outside press or Escape key)
    • ARIA wiring

    Because it is headless, it ships no styles. You must provide your own CSS by targeting the data-* attributes emitted by each part.

  8. Style the Select component using data attributes

    main

    Since Select is headless, you must use the emitted data-* attributes to apply styles.

    State Attributes:

    • data-open: Present on Trigger and Popup when the dropdown is open.
    • data-closed: Present on Trigger and Popup when closed (Note: Popup stays mounted to allow exit animations).
    • data-selected: Present on Option for the currently selected item.
    • data-active: Present on Option for the item currently highlighted via keyboard.
    • data-disabled: Present on Option for disabled items.
    • data-starting-style: Present on Popup during the entering frame.
    • data-ending-style: Present on Popup during the exit animation.

    Positioning Attributes:

    • data-side: Present on Positioner and Arrow (values: top, bottom, left, right).
  9. The `Appearance` object and component overrides

    main

    The Appearance object extends the Theme object by adding specific keys for every Clerk component. This allows you to target the styling of a specific component (like signIn or userButton) within a global appearance configuration.

    Every Clerk component has a corresponding key in the Appearance object that accepts a Theme object.

    Appearance = Theme & {
      signIn?: Theme
      signUp?: Theme
      userButton?: Theme
      userProfile?: Theme
      organizationSwitcher?: Theme
      organizationProfile?: Theme
      // ... every Clerk component has a key
    }
  10. Use the Organization Profile Profile Section component

    main

    The Organization Profile Profile Section is a specialized component designed to manage the lifecycle of editing an organization's identity. It handles the open, editing, and saving states for an organization's name and slug.

    Key Features:

    • State Management: It manages the UI state for opening a dialog, entering edit mode, and the saving process.
    • Drafting Mechanism: Edits are treated as machine-owned drafts. The form seeds itself with current organization values and only commits changes upon a successful save.
    • Dialog Integration: It automatically wraps the edit form in a Dialog component.
    • Automatic Closure: The component is designed to close the editing dialog automatically once a save operation succeeds, without requiring manual syncing effects.
  11. How FileUpload components and hooks work together

    main

    The FileUpload primitive uses a provider/consumer pattern.

    1. FileUpload.Root: Acts as the provider. It owns the file state, the hidden input, and provides the context for all sub-components.
    2. FileUpload.useFileUpload(): A hook used inside FileUpload.Root to access the current state and control methods. It returns:
      • files: The current list of selected File objects.
      • addFiles: Method to add files.
      • removeFile: Method to remove a specific file.
      • clearFiles: Method to clear all files.
      • openFilePicker: Method to programmatically open the file picker.
      • disabled: Boolean indicating if the uploader is disabled.
    3. Parts: Components like Trigger, Dropzone, and Item consume the context from Root to perform their specific roles (e.g., opening the picker or deleting an item).
  12. Resolve labels for SSR using the items prop

    main

    To ensure Select.Value can display the correct label during Server-Side Rendering (SSR) or before the actual Select.Option components have mounted, provide an items array to Select.Root.

    Note: The items prop is only for label resolution; it does not automatically render the options. You must still manually render the Select.Option children.

    const items = [
      { value: 'apple', label: 'Apple' },
      { value: 'banana', label: 'Banana' },
    ];
    
    <Select.Root
      items={items}
      defaultValue='apple'
    >
      {/* Select.Value will display "Apple" even before Options mount */}
    </Select.Root>;