Skip to content

Latest commit

 

History

History
197 lines (151 loc) · 9.24 KB

README.md

File metadata and controls

197 lines (151 loc) · 9.24 KB

@crossly/react-native

npm CI Expo types license

The Crossly storefront in a React Native or Expo app, drawn with native views: a product grid, a buy button, and Vault Markets price widgets.

React Native has no DOM and no custom elements, so this package doesn't wrap @crossly/embed. It talks to the same public endpoints that the embed calls, in the same way:

request
catalog GET https://crossly.net/api/embed/v1/catalog?limit=
checkout POST https://crossly.net/api/embed/v1/checkout { items: [{ listingId, quantity }], returnUrl? }
index GET https://crossly.net/api/embed/v1/vault/index/:slug?range=
mark GET https://crossly.net/api/embed/v1/vault/mark?bookKey=&range=

Every request sends your publishable key in x-crossly-key, with credentials: 'omit'. The key is public by design: it can read your own active listings and start a checkout, and nothing else. Payment happens on the Crossly-hosted checkout, in a browser, never in your app.

Install

npm install @crossly/react-native
# Expo, for an in-app browser sheet at checkout (optional):
npx expo install expo-web-browser

Peers: react and react-native (0.74+). No native modules, no linking, no pods.

Quick start

Mint a key in Crossly → Settings → Embeds. Leave its origin allowlist empty (see Keys and origins).

import * as WebBrowser from 'expo-web-browser';
import { CatalogGrid, CrosslyProvider } from '@crossly/react-native';

export default function Shop() {
  return (
    <CrosslyProvider pk="crossly_pk_live_…" theme={{ accent: '#ff5a5f' }}>
      <CatalogGrid
        limit={24}
        numColumns={2}
        showBuyButtons
        buyButtonProps={{ openUrl: (url) => WebBrowser.openBrowserAsync(url) }}
        onPressListing={(listing) => console.log(listing.id)}
        contentContainerStyle={{ padding: 16 }}
      />
    </CrosslyProvider>
  );
}

A complete Expo app is in examples/expo.

Components

All components take pk and api (overriding the provider), theme (partial CrosslyTheme), style and testID.

<CatalogGrid>

A FlatList of your active listings. It shows the same fields and wording as <crossly-catalog>: a square image, the title, brand · size · condition, and the price, or "Sold". It shows "Nothing listed right now." when empty and the API's message on error.

prop type default
limit number 12 1–48.
numColumns number 2
onPressListing (listing: EmbedListing) => void Makes cards pressable.
showBuyButtons boolean false A <BuyButton> in each available card.
buyButtonProps Omit<BuyButtonProps, 'listing'> E.g. openUrl, returnUrl, onCheckout.
renderListing (listing) => ReactElement Replaces the default card (<ListingCard> is exported).
onLoaded ({ listings }) => void Like crossly:loaded. Not called for an empty catalog.
onError ({ message }) => void Like crossly:error.
loadingComponent, emptyComponent ReactNode spinner / text
contentContainerStyle, ListHeaderComponent Passed to the FlatList.

<BuyButton>

Starts a checkout with POST /embed/v1/checkout and opens the returned Crossly-hosted URL.

prop type default
listing string Required. The listing id.
quantity number 1 1–10.
label string "Buy now" Shows "Starting checkout…" while the request runs.
returnUrl string none Where checkout sends the buyer afterwards. It must be on an allowed origin.
openUrl (url) => unknown Linking.openURL E.g. WebBrowser.openBrowserAsync from expo-web-browser.
onCheckout ({ checkoutUrl }, event) => void Called before openUrl. event.preventDefault() stops the default open.
onError ({ message }) => void The API's message, e.g. "Not available: … They may have sold or been delisted."
textStyle TextStyle

The button is disabled without a key or listing, and while a request is in flight.

<PriceIndex> / <PriceMark>

The Vault Markets widgets, as <crossly-index> / <crossly-mark> render them: name (a link), the level and the move, or the mark, then Quality X · n … · as of … and a Data: Crossly link. They show "Insufficient data" or "No market yet" when the API says the data is too thin. There's no sparkline yet: the points are on the payload if you want to draw one.

prop type
slug (PriceIndex) / bookKey (PriceMark) string Required. Index slug, or <variantId>:<condition>:<grade>.
range '1m' | '3m' | '6m' | '1y' | '5y' Default 1y.
openUrl (url) => unknown Opens href. Default Linking.openURL.
onLoaded ({ index }) / ({ mark })
onError ({ message })

Hooks and client

export
useCatalog({ limit?, pk?, api? }) { data, loading, error, reload }
usePriceIndex(slug, { range? }), usePriceMark(bookKey, { range? }) same shape
useCheckout() { start(items, returnUrl?) → Promise<{ checkoutUrl, mode }>, pending, error }
createCrosslyClient({ pk, api?, fetch? }) catalog, priceIndex, priceMark, checkout. Throws CrosslyApiError (status, code, message).
formatPrice(cents, currency?, locale?), formatMove(bps) Same output as the embed's helpers.
useCrosslyClient(), useCrosslyConfig(), useCrosslyTheme()

Types: EmbedListing, EmbedIndexWidget, EmbedMarkWidget, CrosslyRange, CheckoutItem, CheckoutSession, CrosslyTheme, and every *Props.

Theming

CrosslyTheme mirrors the embed's CSS variables, with the same light and dark defaults. It follows the system colour scheme through useColorScheme:

key CSS variable light dark
gap --crossly-gap 16
radius --crossly-radius 10
fg --crossly-fg #111 #f3f4f6
muted --crossly-muted #666 #9ca3af
accent --crossly-accent #10b981
accentFg (button text) #fff
cardBg --crossly-card-bg #fff #111827
border --crossly-border #e5e7eb #374151
up / down (move colours) #059669 / #dc2626

Set it app-wide with <CrosslyProvider theme> or per component with theme. Use style, renderListing and textStyle for the rest.

Keys and origins

A browser sends an Origin header, and the API checks it against the key's allowlist. A React Native fetch sends no Origin. An empty allowlist permits any origin, so it also permits app requests. A key with an allowlist will answer 403 ("This key is not allowed from an unknown origin"). Use a separate, unrestricted key for your app. A returnUrl, if you send one, is validated against the same allowlist.

Expo

Expo Go works as is: the package uses only React Native core (FlatList, Image, Pressable, Linking, useColorScheme). expo-web-browser is optional. Pass its openBrowserAsync as openUrl to keep the buyer in a browser sheet.

Not included (yet)

  • Text search (q). The API requires a proof-of-work header for fresh searches, and the 0.1.0 embed doesn't implement it either, so it isn't offered here.
  • Pagination. The 0.1.0 embed shows the first page (limit up to 48), and so does this package.
  • Sparklines in the price widgets (points is on the payload).

Why @crossly/embed is a dependency

It's a regular dependency, used for types only (EmbedListing, EmbedIndexWidget, EmbedMarkWidget). Those types are the contract of the endpoints this package calls, so they come from the embed rather than a copy that could drift. The imports are import type, so none of the embed's DOM code reaches your bundle.

Jest in your app

dist/ is CommonJS plus .d.ts, so it works under the React Native Jest preset without a transformIgnorePatterns change.

Links

License

MIT