Skip to content

Commit

Permalink
feat(base): new preview components using Sanity UI (#2749)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjelfull committed Sep 10, 2021
1 parent be20701 commit 3696728
Show file tree
Hide file tree
Showing 26 changed files with 905 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ interface BlockImagePreviewProps {
const DEFAULT_MEDIA_DIMENSIONS: MediaDimensions = {width: 600, height: 600, fit: 'fillmax'}

export default class BlockImagePreview extends React.PureComponent<BlockImagePreviewProps> {
componentDidMount() {
// eslint-disable-next-line no-console
console.warn(
"BlockImagePreview from `part:@sanity/components/previews/block-image` is deprecated. Please use `import {BlockImagePreview} from '@sanity/base/components'` instead"
)
}
// eslint-disable-next-line complexity
render() {
const {title, subtitle, description, mediaDimensions, media, children, status} = this.props
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const DEFAULT_MEDIA_DIMENSIONS: MediaDimensions = {
}

export default class BlockPreview extends React.PureComponent<BlockPreviewProps> {
componentDidMount() {
// eslint-disable-next-line no-console
console.warn(
"BlockPreview from `part:@sanity/components/previews/block` is deprecated. Please use `import {BlockPreview} from '@sanity/base/components'` instead"
)
}
render() {
const {
title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const svgStyles: React.CSSProperties = {
height: '100%',
}

// @todo remove this component in next major release

export default class CardPreview extends React.PureComponent<CardPreviewProps> {
static defaultProps = {
title: 'Untitled',
Expand All @@ -58,6 +60,13 @@ export default class CardPreview extends React.PureComponent<CardPreviewProps> {

dateElement: HTMLDivElement | null = null

componentDidMount() {
// eslint-disable-next-line no-console
console.warn(
'CardPreview from `part:@sanity/components/previews/card` is deprecated, and will be removed in a future update.'
)
}

// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.isPlaceholder && this.dateElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DefaultPreview extends React.PureComponent<DefaultPreviewProps> {
componentDidMount() {
// eslint-disable-next-line no-console
console.warn(
"DefaultPreview from `part:@sanity/components/previews/default` is deprecated. Please `import { DefaultPreview } from '@sanity/base/components'` instead"
"DefaultPreview from `part:@sanity/components/previews/default` is deprecated. Please use `import {DefaultPreview} from '@sanity/base/components'` instead"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ const DEFAULT_MEDIA_DIMENSIONS: MediaDimensions = {
export default class DetailPreview extends React.PureComponent<DetailPreviewProps> {
index = index++

componentDidMount() {
// eslint-disable-next-line no-console
console.warn(
"DetailPreview from `part:@sanity/components/previews/detail` is deprecated. Please use `import {DetailPreview} from '@sanity/base/components'` instead"
)
}

// eslint-disable-next-line complexity
render() {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const DEFAULT_MEDIA_DIMENSIONS: MediaDimensions = {
}

export default class InlinePreview extends React.PureComponent<InlinePreviewProps> {
componentDidMount() {
// eslint-disable-next-line no-console
console.warn(
"InlinePreview from `part:@sanity/components/previews/inline` is deprecated. Please use `import {InlinePreview} from '@sanity/base/components'` instead"
)
}
// eslint-disable-next-line complexity
render() {
const {title, media, mediaDimensions = DEFAULT_MEDIA_DIMENSIONS, children} = this.props
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const DEFAULT_MEDIA_DIMENSIONS: MediaDimensions = {
}

export default class MediaPreview extends React.PureComponent<MediaPreviewProps> {
componentDidMount() {
// eslint-disable-next-line no-console
console.warn(
"MediaPreview from `part:@sanity/components/previews/media` is deprecated. Please use `import {MediaPreview} from '@sanity/base/components'` instead"
)
}
// eslint-disable-next-line complexity
render() {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ interface ProgressCircleProps {
}

export default class ProgressCircle extends React.PureComponent<ProgressCircleProps> {
componentDidMount() {
// eslint-disable-next-line no-console
console.warn(
"ProgressCircle from `part:@sanity/components/progress/circle` is deprecated. Please use `import {ProgressCircle} from '@sanity/base/components'` instead"
)
}
render() {
const {percent = 0, text, style, showPercent, isComplete} = this.props

Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/base/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './previews'
export * from './IntentButton'
export * from './IntentLink'
export * from './validation'
export * from './progress'
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {Box} from '@sanity/ui'
import styled from 'styled-components'

export const MediaWrapper = styled(Box)`
position: relative;
flex: 1;
img {
display: block;
width: 100%;
height: auto;
pointer-events: none;
}
svg {
display: block;
width: calc(2.5rem + 1px) !important;
height: calc(2.5rem + 1px) !important;
margin: 0.5rem;
}
`

export const Root = styled(Box)`
user-select: none;
width: 100%;
`

export const MetadataWrapper = styled(Box)`
user-select: none;
width: 100%;
${MediaWrapper} + &:empty {
display: none;
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react'
import {Box, Flex, Heading, Text} from '@sanity/ui'
import {getDevicePixelRatio} from 'use-device-pixel-ratio'
import {MediaDimensions, PreviewProps} from './types'
import {Root, MediaWrapper, MetadataWrapper} from './blockImagePreview.styled'

const DEFAULT_MEDIA_DIMENSIONS: MediaDimensions = {
width: 600,
height: 600,
fit: 'fillmax',
dpr: getDevicePixelRatio(),
}

// @todo This is to make sure there is the correct amount of spacing below the dropdown in `BlockObjectPreview`. Remove when `BlockObjectPreview` is migrated to Sanity UI.
const STYLE_HEADING = {marginBottom: '2px'}

export const BlockImagePreview: React.FunctionComponent<PreviewProps<'block'>> = (props) => {
const {title, subtitle, description, mediaDimensions, media, children, status} = props
return (
<Root>
{title && (
<Box as="header" paddingY={4} paddingX={[3, 4]} marginTop={1}>
<Heading textOverflow="ellipsis" size={1} style={STYLE_HEADING}>
{title}
</Heading>
</Box>
)}

<Flex justify="center" direction="column">
{media && (
<MediaWrapper>
{typeof media === 'function' &&
media({
dimensions: mediaDimensions || DEFAULT_MEDIA_DIMENSIONS,
layout: 'block',
})}
{typeof media === 'string' && <Box>{media}</Box>}
{React.isValidElement(media) && media}
</MediaWrapper>
)}

{subtitle || description || status || (
<MetadataWrapper paddingX={2} paddingY={2}>
{subtitle && (
<Text muted size={1} textOverflow="ellipsis">
{subtitle}
</Text>
)}
{description && (
<Box marginTop={2}>
<Text muted size={1} textOverflow="ellipsis">
{typeof description === 'function' ? description({layout: 'block'}) : description}
</Text>
</Box>
)}
{status && (
<Box marginTop={2}>
{typeof status === 'function' ? status({layout: 'block'}) : status}
</Box>
)}
</MetadataWrapper>
)}
</Flex>

{children && <Box>{children}</Box>}
</Root>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {Box, Flex} from '@sanity/ui'
import styled from 'styled-components'

export const Root = styled(Box)`
user-select: none;
position: relative;
width: 100%;
`

export const ContentWrapper = styled(Box)``

export const Header = styled(Flex)`
flex-grow: 1;
`

export const MediaWrapper = styled(Box)`
height: calc(2.5rem + 1px);
width: calc(2.5rem + 1px);
min-width: calc(2.5rem + 1px);
position: relative;
overflow: hidden;
img {
width: 100%;
height: 100%;
display: block;
object-fit: cover;
}
svg {
display: block;
width: calc(2.5rem + 1px) !important;
height: calc(2.5rem + 1px) !important;
}
`
73 changes: 73 additions & 0 deletions packages/@sanity/base/src/components/previews/blockPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React, {createElement} from 'react'
import {Box, Text} from '@sanity/ui'
import {getDevicePixelRatio} from 'use-device-pixel-ratio'
import {MediaDimensions, PreviewProps} from './types'
import {MediaWrapper, Root, Header, ContentWrapper} from './blockPreview.styled'

const DEFAULT_MEDIA_DIMENSIONS: MediaDimensions = {
width: 40,
height: 40,
aspect: 1,
fit: 'crop',
dpr: getDevicePixelRatio(),
}

export const BlockPreview: React.FunctionComponent<PreviewProps<'block'>> = (props) => {
const {
title,
subtitle,
description,
mediaDimensions,
media,
status,
children,
extendedPreview,
} = props
return (
<Root>
<Header padding={2} align="center">
{media && (
<MediaWrapper marginRight={3}>
{typeof media === 'function' &&
media({
dimensions: mediaDimensions || DEFAULT_MEDIA_DIMENSIONS,
layout: 'block',
})}
{typeof media === 'string' && <Box>{media}</Box>}
{React.isValidElement(media) && media}
</MediaWrapper>
)}

<ContentWrapper flex={1} paddingY={1}>
<Text textOverflow="ellipsis" style={{color: 'inherit'}}>
{title && typeof title === 'function' ? title({layout: 'block'}) : title}
{!title && <>Untitled</>}
</Text>

{subtitle && (
<Box marginTop={1}>
<Text muted size={1} textOverflow="ellipsis">
{typeof subtitle === 'function' ? subtitle({layout: 'block'}) : subtitle}
</Text>
</Box>
)}
{description && (
<Box marginTop={3}>
<Text muted size={1} textOverflow="ellipsis">
{typeof description === 'function' ? description({layout: 'block'}) : description}
</Text>
</Box>
)}
</ContentWrapper>

{status && (
<Box padding={3}>{typeof status === 'function' ? status({layout: 'block'}) : status}</Box>
)}
</Header>

{children && <Box>{children}</Box>}

{extendedPreview && <Box>{extendedPreview}</Box>}
</Root>
)
}
16 changes: 4 additions & 12 deletions packages/@sanity/base/src/components/previews/defaultPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const MediaWrapper = styled(Flex)`
& img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
inset: 0;
object-fit: cover;
border-radius: inherit;
}
Expand Down Expand Up @@ -92,18 +89,13 @@ export const DefaultPreview = (props: PreviewProps<'default'>) => {

<Stack space={2} flex={1}>
<Text textOverflow="ellipsis" style={{color: 'inherit'}}>
{title && (
<>
{typeof title !== 'function' && title}
{typeof title === 'function' && title({layout: 'default'})}
</>
)}
{title && typeof title === 'function' ? title({layout: 'default'}) : title}
{!title && <>Untitled</>}
</Text>

{subtitle && (
<Text muted size={1} textOverflow="ellipsis">
{(typeof subtitle === 'function' && subtitle({layout: 'default'})) || subtitle}
{typeof subtitle === 'function' ? subtitle({layout: 'default'}) : subtitle}
</Text>
)}

Expand All @@ -112,7 +104,7 @@ export const DefaultPreview = (props: PreviewProps<'default'>) => {

{status && (
<Box padding={3}>
{(typeof status === 'function' && status({layout: 'default'})) || status}
{typeof status === 'function' ? status({layout: 'default'}) : status}
</Box>
)}
</>
Expand Down

2 comments on commit 3696728

@vercel
Copy link

@vercel vercel bot commented on 3696728 Sep 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 3696728 Sep 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio-git-next.sanity.build
perf-studio.sanity.build

Please sign in to comment.