Skip to content

Commit

Permalink
dev(test-studio): add components API schema and plugins + update `san…
Browse files Browse the repository at this point in the history
…ity.config`
  • Loading branch information
hermanwikner committed Oct 6, 2022
1 parent 85a8f10 commit 0a7ce76
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 24 deletions.
71 changes: 71 additions & 0 deletions dev/test-studio/components/formComponents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react'
import {Card} from '@sanity/ui'
import {createPlugin, InputProps, FieldProps, ItemProps, PreviewProps} from 'sanity'

export function Input(props: InputProps) {
return (
<Card data-testid="input-config-component" padding={2} border tone="primary">
{props.renderDefault(props)}
</Card>
)
}

export function Field(props: FieldProps) {
return (
<Card data-testid="field-config-component" padding={2} border tone="caution">
{props.renderDefault(props)}
</Card>
)
}

export function Item(props: ItemProps) {
return (
<Card data-testid="item-config-component" padding={2} border tone="positive">
{props.renderDefault(props)}
</Card>
)
}

export function Preview(props: PreviewProps) {
return (
<Card data-testid="preview-config-component" padding={2} border tone="critical">
{props.renderDefault(props)}
</Card>
)
}

export const formComponentsPlugin = createPlugin({
name: 'form-components-plugin',
form: {
components: {
input: (props) => {
return (
<Card data-testid="input-plugin-component" padding={2} border tone="primary">
{props.renderDefault(props)}
</Card>
)
},
field: (props) => {
return (
<Card data-testid="field-plugin-component" padding={2} border tone="caution">
{props.renderDefault(props)}
</Card>
)
},
item: (props) => {
return (
<Card data-testid="item-plugin-component" padding={2} border tone="positive">
{props.renderDefault(props)}
</Card>
)
},
preview: (props) => {
return (
<Card data-testid="preview-plugin-component" padding={2} border tone="critical">
{props.renderDefault(props)}
</Card>
)
},
},
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import React, {createContext, useContext} from 'react'
import {Box, Card, Flex, Stack, Text} from '@sanity/ui'
import {LogoProps, NavbarProps, LayoutProps, createPlugin, ToolMenuProps} from 'sanity'

export const componentsPlugin = createPlugin({
name: 'components-plugin',
export const studioComponentsPlugin = createPlugin({
name: 'studio-components-plugin',
studio: {
components: {
Layout: (props) => (
layout: (props) => (
<Box height="fill" data-testid="test-layout-plugin">
{props.renderLayout(props)}
{props.renderDefault(props)}
</Box>
),
Logo: (props) => <Box data-testid="test-logo-plugin">{props.renderLogo(props)}</Box>,
Navbar: (props) => <Box data-testid="test-navbar-plugin">{props.renderNavbar(props)}</Box>,
ToolMenu: (props) => (
<Box data-testid="test-tool-menu-plugin">{props.renderToolMenu(props)}</Box>
logo: (props) => <Box data-testid="test-logo-plugin">{props.renderDefault(props)}</Box>,
navbar: (props) => <Box data-testid="test-navbar-plugin">{props.renderDefault(props)}</Box>,
toolMenu: (props) => (
<Box data-testid="test-tool-menu-plugin">{props.renderDefault(props)}</Box>
),
},
},
Expand All @@ -25,12 +25,12 @@ const TitleContext = createContext<string>('')
const useTitleContext = () => useContext(TitleContext)

export function CustomLayout(props: LayoutProps) {
const {renderLayout} = props
const {renderDefault} = props

return (
<TitleContext.Provider value="Context value">
<Box height="fill" data-testid="test-layout-config">
{renderLayout(props)}
{renderDefault(props)}
</Box>
</TitleContext.Provider>
)
Expand All @@ -40,7 +40,7 @@ export function CustomLayout(props: LayoutProps) {
export function CustomLogo(props: LogoProps) {
const title = useTitleContext()

return props.renderLogo({...props, title})
return props.renderDefault({...props, title})
}

// Navbar
Expand All @@ -50,13 +50,13 @@ export function CustomNavbar(props: NavbarProps) {
<Card padding={4} tone="primary" data-testid="test-navbar-banner-config">
<Flex align="center" gap={4}>
<Text weight="semibold" size={1}>
This banner is rendered with <code>{`components.Navbar`}</code> in{' '}
This banner is rendered with <code>{`components.navbar`}</code> in{' '}
<code>{`createConfig`}</code>
</Text>
</Flex>
</Card>

{props.renderNavbar(props)}
{props.renderDefault(props)}
</Stack>
)
}
Expand All @@ -73,7 +73,7 @@ export function CustomToolMenu(props: ToolMenuProps) {
sizing="border"
tone="primary"
>
{props.renderToolMenu(props)}
{props.renderDefault(props)}
</Card>
)
}
1 change: 1 addition & 0 deletions dev/test-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@portabletext/react": "^1.0.6",
"@sanity/icons": "1.3.7-next.18",
"@sanity/color": "2.1.17-next.18",
"@sanity/image-url": "^1.0.1",
"@sanity/portable-text-editor": "3.0.0-dev-preview.20",
"@sanity/types": "3.0.0-dev-preview.20",
Expand Down
27 changes: 18 additions & 9 deletions dev/test-studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
CustomLayout,
CustomNavbar,
CustomToolMenu,
componentsPlugin,
} from './components/customComponents'
studioComponentsPlugin,
} from './components/studioComponents'
import {Field, formComponentsPlugin, Input, Item, Preview} from './components/formComponents'

const sharedSettings = createPlugin({
name: 'sharedSettings',
Expand Down Expand Up @@ -80,7 +81,7 @@ export default createConfig([
basePath: '/test',
studio: {
components: {
Logo: Branding,
logo: Branding,
},
},
},
Expand All @@ -93,7 +94,7 @@ export default createConfig([
basePath: '/playground',
studio: {
components: {
Logo: Branding,
logo: Branding,
},
},
},
Expand All @@ -102,14 +103,22 @@ export default createConfig([
title: 'Test Studio (custom-components)',
projectId: 'ppsg7ml5',
dataset: 'test',
plugins: [sharedSettings(), componentsPlugin()],
plugins: [sharedSettings(), studioComponentsPlugin(), formComponentsPlugin()],
basePath: '/custom-components',
form: {
components: {
input: Input,
field: Field,
item: Item,
preview: Preview,
},
},
studio: {
components: {
Layout: CustomLayout,
Logo: CustomLogo,
Navbar: CustomNavbar,
ToolMenu: CustomToolMenu,
layout: CustomLayout,
logo: CustomLogo,
navbar: CustomNavbar,
toolMenu: CustomToolMenu,
},
},
},
Expand Down
88 changes: 88 additions & 0 deletions dev/test-studio/schema/docs/v3/form-components-api/components.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import React from 'react'
import {hues} from '@sanity/color'
import {Box, Stack, Heading, Flex, Inline, Text} from '@sanity/ui'
import {FieldProps, InputProps, ItemProps, PreviewProps} from 'sanity'

const COMPONENT_COLORS = {
input: hues.blue[400].hex,
field: hues.yellow[400].hex,
preview: hues.red[400].hex,
item: hues.green[400].hex,
}

export function FormInput(props: InputProps) {
return (
<Stack space={5} padding={3}>
<Stack space={4}>
<Stack space={4}>
<Heading>Form components API test</Heading>
<Text size={1}>
The borders are configured in the schema, and the backgrounds are configured in the
config and in a plugin.
</Text>
</Stack>

<Flex align="center" gap={4}>
{Object.entries(COMPONENT_COLORS).map(([key, value]) => (
<Inline space={2} key={key}>
<div style={{width: '1em', height: '1em', background: value, borderRadius: '50%'}} />
<Text size={1} weight="semibold">
{key.charAt(0).toUpperCase() + key.slice(1)}
</Text>
</Inline>
))}
</Flex>
</Stack>

<CustomInput {...props} testId="" />
</Stack>
)
}

export function CustomField(props: FieldProps & {testId: string}) {
return (
<Box
data-testid={props.testId}
padding={2}
style={{border: `4px solid ${COMPONENT_COLORS.field}`}}
>
{props.renderDefault(props)}
</Box>
)
}

export function CustomInput(props: InputProps & {testId: string}) {
return (
<Box
data-testid={props.testId}
padding={2}
style={{border: `4px solid ${COMPONENT_COLORS.input}`}}
>
{props.renderDefault(props)}
</Box>
)
}

export function CustomItem(props: ItemProps & {testId: string}) {
return (
<Box
data-testid={props.testId}
padding={2}
style={{border: `4px solid ${COMPONENT_COLORS.item}`}}
>
{props.renderDefault(props)}
</Box>
)
}

export function CustomPreview(props: PreviewProps & {testId: string}) {
return (
<Box
data-testid={props.testId}
padding={2}
style={{border: `4px solid ${COMPONENT_COLORS.preview}`}}
>
{props.renderDefault(props)}
</Box>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './schema'
Loading

0 comments on commit 0a7ce76

Please sign in to comment.