@molecule/app-oauth-buttons-svelte

Feature · oauth-buttons · App (browser) · v1.2.1 · Apache-2.0

Config-driven OAuth provider button row composing @molecule/app-oauth-logos-svelte with layout variants (horizontal / vertical / grid) and brand-themed inline colors

npm install @molecule/app-oauth-buttons-svelte

npm · Source on GitHub

How it works

@molecule/app-oauth-buttons-svelte is a ready-made oauth-buttons feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

<script lang="ts">
  import { OAuthButtons } from '@molecule/app-oauth-buttons-svelte'
  import { useOAuth } from '@molecule/app-svelte'
  import { oauthConfig } from '../config.js'

  const { providers, redirect } = useOAuth(oauthConfig)
</script>

<OAuthButtons {providers} onSelect={redirect} layout="grid" showLabels />

Works with: @molecule/app-oauth-logos-svelte, @molecule/app-svelte, @molecule/app-ui

Reference

Config-driven OAuth provider button row — the Svelte counterpart of @molecule/app-oauth-buttons-react, with the same components, the same props and the same rendered DOM.

Lifts the bespoke OAuth button row re-implemented by every flagship Login / Signup page into a single composable component, building on @molecule/app-oauth-logos-svelte for the canonical brand marks.

  • providers accepts the canonical OAuthProviderId[] from useOAuth(config).providers (or any other source) — apps no longer need to map provider strings into bespoke button arrays.
  • onSelect(provider) initiates the OAuth flow. Host apps typically pass redirect from useOAuth(config) or signInWithProvider from their auth bond.
  • layout toggles 'horizontal' | 'vertical' | 'grid' — the grid variant auto-paginates into a 2-column layout above 4 providers.
  • brandButtons opt-in paints each button with its provider's exact brand-spec background (#fff for Google, #24292f for GitHub, etc.) via the inline style attribute — those are provider-mandated color tokens ClassMap intentionally does not encode. It is independent of iconMode (logo color). Layout, padding, radius, and chrome all come from the wired ClassMap (cm.oauthButtonGroup, cm.oauthButton, cm.oauthButtonIcon).
  • OAuthDivider is the composable "or continue with" rule — the config-driven OAuth row in @molecule/app-auth-ui-svelte composes it above this row.

Companion locale bond: @molecule/app-locales-oauth-buttons (79 languages).

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

<script lang="ts">
  import { OAuthButtons } from '@molecule/app-oauth-buttons-svelte'
  import { useOAuth } from '@molecule/app-svelte'
  import { oauthConfig } from '../config.js'

  const { providers, redirect } = useOAuth(oauthConfig)
</script>

<OAuthButtons {providers} onSelect={redirect} layout="grid" showLabels />

Type

feature

Installation

npm install @molecule/app-oauth-buttons-svelte @molecule/app-oauth-logos-svelte @molecule/app-svelte @molecule/app-ui svelte

API

Interfaces

BrandStyle

Style payload for a single brand button (the data behind BRAND_STYLES).

interface BrandStyle {
  /** Background color (CSS color token). */
  background: string
  /** Foreground / label color (CSS color token). */
  color: string
  /** Optional 1px border color when `background` is white/very light. */
  borderColor?: string
}

OAuthButtonsProps

Props accepted by OAuthButtons.

interface OAuthButtonsProps {
  /**
   * Ordered list of provider ids to render.
   *
   * Typically the `string[]` from `createOAuthStores(config).providers`. Known
   * canonical ids (e.g. `'google'`, `'github'` — see `OAuthProviderId`)
   * get their brand logo + localized label; unknown ids fall through to
   * a synthesized label and a ClassMap-neutral button, so any string is
   * safe to pass.
   */
  providers: string[]
  /**
   * Optional click handler. Called with the provider id when the user
   * activates a button.
   *
   * If omitted, buttons render as plain `<button type="button">` with no
   * default behavior — host apps wire the handler (typically calling
   * `redirect(provider)` from `useOAuth(...)` or an auth bond's inline
   * `signInWithProvider(provider)`).
   *
   * May be sync or async. A fire-and-navigate `redirect` returns `void`;
   * an inline flow (popup / PKCE / an auth bond that settles the session
   * in place) returns a `Promise` that resolves when the handshake
   * completes — in that case `onSuccess(provider)` fires on resolve.
   */
  onSelect?: (provider: string) => void | Promise<unknown>
  /**
   * Called with the provider id when the OAuth handshake completes
   * successfully — but ONLY when `onSelect` returns a `Promise` that
   * resolves (an inline / popup / PKCE flow, e.g. an auth bond's
   * `signInWithProvider` that settles the session in place). The
   * component awaits that promise and invokes `onSuccess` on resolve.
   *
   * A full-page `redirect(provider)` onSelect returns `void` and
   * navigates away, so there is nothing to await and `onSuccess` does
   * NOT fire — that flow's completion is observed on the callback page by
   * `useOAuth(config).onSuccess` instead. A rejected handshake also does
   * not fire it.
   */
  onSuccess?: (provider: string) => void
  /**
   * Layout variant. Defaults to `'horizontal'` (flex-wrap row).
   *
   * - `'horizontal'`: row that wraps when crowded.
   * - `'vertical'`: stacked column (one button per line).
   * - `'grid'`: 2-column grid (recommended for >4 providers).
   */
  layout?: OAuthButtonsLayout
  /** Icon size in pixels. Defaults to 30. */
  iconSize?: number
  /** Logo color mode — `'brand'` (default, official multi-color) or `'mono'`. */
  iconMode?: 'brand' | 'mono'
  /**
   * When true, paint each button with its provider's brand-spec
   * background/foreground colors (Google white, GitHub `#24292f`, etc.)
   * via inline `style`. Defaults to `false` — buttons stay
   * ClassMap-neutral so the row is visually uniform.
   *
   * Independent of `iconMode`: `iconMode` controls the *logo's* color
   * rendering, `brandButtons` controls the *button surface*.
   */
  brandButtons?: boolean
  /**
   * When true, render the localized provider label text next to the
   * logo (e.g. `"Continue with GitHub"`). Defaults to `false` (icon-only
   * pixel-identical row across providers).
   */
  showLabels?: boolean
  /** Extra class composed onto the button-group element. */
  className?: string
}

OAuthDividerProps

Props accepted by OAuthDivider — the "or continue with" rule rendered above an OAuth button row.

interface OAuthDividerProps {
  /**
   * i18n key for the divider label. Defaults to `'oauth.orContinueWith'`.
   */
  labelKey?: string
  /**
   * Fallback divider text if the i18n key is missing. Defaults to
   * `'or continue with'`.
   */
  labelDefault?: string
  /** Extra class composed onto the divider wrapper. */
  className?: string
}

Types

OAuthButtonsLayout

Layout variants for the OAuth button row.

type OAuthButtonsLayout = 'horizontal' | 'vertical' | 'grid'

OAuthProviderId

Canonical list of supported providers.

type OAuthProviderId =
  | 'github'
  | 'gitlab'
  | 'google'
  | 'twitter'
  | 'x'
  | 'apple'
  | 'facebook'
  | 'microsoft'
  | 'linkedin'
  | 'discord'

Functions

getBrandStyle(provider)

Returns the inline style attribute value for a given provider id, in Svelte's inline-style format (a CSS declaration string for style={…}).

Unknown providers receive an empty string so the wired ClassMap's default surface color governs — matching the icon-only fallback behavior of OAuthProviderLogo's fallback snippet.

function getBrandStyle(provider: string): string
  • provider — Canonical provider id (e.g. 'google').

Returns: CSS declarations for the button element's style attribute.

getProviderLabel(provider)

Returns the i18n entry (key + English default) for a given provider id.

Falls back to the bond's one literal fallback key for unknown ids so unrecognized providers still render with a sensible English label rather than a raw key string. The provider id travels as a {{provider}} interpolation value, never inside the key.

function getProviderLabel(provider: string): {
  key: string
  default: string
  values?: Record<string, string>
}
  • provider — Canonical provider id (e.g. 'google').

Returns: Translation key, English default and optional interpolation values.

dedupeProviders(providers)

De-duplicates a provider list while preserving the caller's order.

Mirrors how host apps typically pass providers derived from useOAuth(config).providers plus per-page overrides — duplicates can sneak in via merge-and-pass patterns.

function dedupeProviders<T extends string>(providers: readonly T[]): T[]
  • providers — Raw list (may contain duplicates).

Returns: Ordered list with duplicates removed.

getLayoutStyle(layout, providerCount)

Returns the inline style attribute value applied on top of cm.oauthButtonGroup for a given layout variant, in Svelte's inline-style format (a CSS declaration string for style={…}).

function getLayoutStyle(layout: OAuthButtonsLayout, providerCount: number): string
  • layout — Selected layout variant.
  • providerCount — Number of buttons rendered (drives grid columns).

Returns: CSS declarations for the wrapper element's style attribute.

getButtonLayoutStyle(layout)

Returns the inline style attribute value for an individual button in a given layout, in Svelte's inline-style format. In 'vertical' mode each button stretches to fill the column.

function getButtonLayoutStyle(layout: OAuthButtonsLayout): string
  • layout — Selected layout variant.

Returns: CSS declarations for the button element's style attribute.

Constants

OAuthButtons

const OAuthButtons: LegacyComponentType

OAuthDivider

const OAuthDivider: LegacyComponentType

BRAND_STYLES

Brand-spec colors for each supported OAuth provider.

Sourced from each provider's official developer brand guidelines. When a host app wants to override these (dark-mode tweaks, brand exceptions), pass iconMode="mono" and let the wired ClassMap paint everything via cm.oauthButton.

const BRAND_STYLES: Readonly<Record<OAuthProviderId, BrandStyle>>

PROVIDER_LABELS

Canonical i18n key + English default for each supported provider.

Used by OAuthButtons to render the localized provider name (e.g. "Continue with {{provider}}"). The key matches the companion locale bond @molecule/app-locales-oauth-buttons.

const PROVIDER_LABELS: Readonly<Record<OAuthProviderId, { key: string; default: string }>>

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-oauth-logos-svelte ^1.0.0
  • @molecule/app-svelte ^3.5.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • svelte ^5.0.0

Runtime Dependencies

  • @molecule/app-oauth-logos-svelte
  • @molecule/app-svelte
  • @molecule/app-ui
  • svelte

Rendering-only: this package draws the buttons; the OAuth handshake itself — authorize redirect, and the callback/code-to-session exchange on return — belongs to useOAuth(config) (which needs the @molecule/app-svelte auth context) or your auth bond's signInWithProvider. onSuccess(provider) fires only for an inline onSelect that returns a Promise resolving on handshake completion (popup / PKCE); a full-page redirect onSelect returns void, so onSuccess does not fire and completion is observed by useOAuth(config).onSuccess on the callback page instead. Requires a wired ClassMap bond and an I18nProvider ancestor in @molecule/app-svelte's molecule context — getClassMap() and useTranslation() both throw before wiring.

Translations

Translation strings are provided by @molecule/app-locales-oauth-buttons.