Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const preview: Preview = {
publishableKey={publishableKey}
userIdentifierKey={userIdentifierKey}
endpoint={seamEndpoint}
disableCssInjection
disableFontInjection
>
<Story />
</SeamProvider>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ $ npm install @seamapi/react
3. Drop in Seam components.

```ts
import '@seamapi/react/index.css'

import { SeamProvider, DeviceTable } from '@seamapi/react'

export const App = () => {
Expand All @@ -54,6 +52,8 @@ export const App = () => {

### Fonts

> The font is automatically included unless you specify `disableFontInjection` in `<SeamProvider />`

The components are optimized for use with [Source Sans Pro], but will fallback to other system sans-serif fonts.
Load the font from Google Fonts by placing the following in the `<head>` tag:

Expand Down
4 changes: 0 additions & 4 deletions examples/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Basic example - Seam React Components</title>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600;700&display=swap"
/>
</head>
<body>
<div id="root"></div>
Expand Down
2 changes: 0 additions & 2 deletions examples/basic/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import '@seamapi/react/index.css'

import { DeviceTable, SeamProvider } from '@seamapi/react'

export const App = (): JSX.Element => {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/SeamProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
} from 'react'
import type { Seam, SeamClientOptions } from 'seamapi'

import { useSeamFont } from './seam/use-seam-font.js'
import { useSeamStyles } from './seam/use-seam-styles.js'

declare global {
// eslint-disable-next-line no-var
var seam: SeamProviderProps | undefined
Expand All @@ -22,10 +25,14 @@ export interface SeamContext {
clientSessionToken?: string | undefined
}

type SeamProviderProps =
type SeamProviderProps = {
disableCssInjection?: boolean | undefined
disableFontInjection?: boolean | undefined
} & (
| SeamProviderPropsWithClient
| (SeamProviderPropsWithPublishableKey & AllowedSeamClientOptions)
| (SeamProviderPropsWithClientSessionToken & AllowedSeamClientOptions)
)

interface SeamProviderPropsWithClient {
client: Seam
Expand Down Expand Up @@ -69,6 +76,9 @@ export function SeamProvider({
)
}

useSeamStyles({ enabled: !(props.disableCssInjection ?? false) })
useSeamFont({ enabled: !(props.disableFontInjection ?? false) })

return (
<div className='seam-components'>
<QueryClientProvider client={queryClientRef.current}>
Expand Down
21 changes: 21 additions & 0 deletions src/lib/seam/use-seam-font.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useEffect } from 'react'

const fontUrl =
'https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600;700&display=swap'

export const useSeamFont = ({ enabled }: { enabled: boolean }) => {
useEffect(() => {
if (globalThis.document == null) return
if (!enabled) return
const linkEl = globalThis.document.querySelector(`link[href="${fontUrl}"]`)

if (linkEl === null) {
const link = globalThis.document.createElement('link')
link.rel = 'stylesheet'
link.type = 'text/css'
link.href = fontUrl

globalThis.document.head.appendChild(link)
}
}, [enabled])
}
21 changes: 21 additions & 0 deletions src/lib/seam/use-seam-styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useEffect } from 'react'

// TODO figure out the version automatically
const cssUrl = 'https://www.unpkg.com/@seamapi/react@1.1.0/index.min.css'

export const useSeamStyles = ({ enabled }: { enabled: boolean }) => {
useEffect(() => {
if (globalThis.document == null) return
if (!enabled) return
const linkEl = globalThis.document.querySelector(`link[href="${cssUrl}"]`)

if (linkEl === null) {
const link = globalThis.document.createElement('link')
link.rel = 'stylesheet'
link.type = 'text/css'
link.href = cssUrl

globalThis.document.head.appendChild(link)
}
}, [enabled])
}
2 changes: 2 additions & 0 deletions test/fixtures/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function Providers({ children }: PropsWithChildren): JSX.Element {
endpoint={globalThis.JEST_SEAM_ENDPOINT}
publishableKey={globalThis.JEST_SEAM_PUBLISHABLE_KEY_1}
userIdentifierKey='some_user'
disableCssInjection
disableFontInjection
>
{children}
</SeamProvider>
Expand Down