Skip to content

Commit

Permalink
Merge pull request #57 from BKWLD/fix-component-return-types
Browse files Browse the repository at this point in the history
Returning JSX.Element rather than ReactElement
  • Loading branch information
weotch authored Oct 28, 2024
2 parents d22ccb9 + 840a4be commit 79a70a1
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 50 deletions.
3 changes: 1 addition & 2 deletions packages/contentful/src/ContentfulVisual.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ReactElement } from 'react'
import ReactVisual from '@react-visual/react'
import { ContentfulAsset, ContentfulVisualProps } from './types/contentfulVisualTypes'
import {
Expand All @@ -17,7 +16,7 @@ import {
// Render a Visual using Contentful data
export default function ContentfulVisual(
props: ContentfulVisualProps
): ReactElement | null {
): JSX.Element | null {

// Destructure some props
const {
Expand Down
15 changes: 10 additions & 5 deletions packages/next/src/NextVisual.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import _Image from "next/image";
const Image = ("default" in _Image ? _Image.default : _Image) as typeof _Image;

import type { ReactElement } from 'react'

import { makeImagePlaceholder } from './lib/placeholder'
import {
VisualWrapper,
Expand All @@ -17,7 +15,7 @@ import { NextVisualProps } from './types/nextVisualTypes'
// Render a Sanity image via Next/Image
export default function NextVisual(
props: NextVisualProps
): ReactElement | null {
): JSX.Element | null {

// Destructure props
const {
Expand Down Expand Up @@ -82,8 +80,15 @@ export default function NextVisual(

// An image rendered within the Visual
function NextImage({
src, sizes, alt, fit, position, priority, loader, placeholderData
}: any): ReactElement {
src,
sizes,
alt,
fit,
position,
priority,
loader,
placeholderData,
}: any): JSX.Element {
return (
<Image
{ ...{ src, sizes, priority, loader, alt } }
Expand Down
7 changes: 2 additions & 5 deletions packages/react/src/LazyVideo/AccessibilityControls.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { LazyVideoProps } from '../types/lazyVideoTypes'
import {
CSSProperties,
type ReactElement,
} from 'react';
import type { CSSProperties } from 'react';
import { PositionOption } from '../types/reactVisualTypes'

// How big to make the button. Can't be too small and still be ADA friendly
Expand Down Expand Up @@ -33,7 +30,7 @@ export default function AccessibilityControls({
pauseIcon,
hideAccessibilityControls,
accessibilityControlsPosition,
}: AccessibilityControlsProps): ReactElement | null {
}: AccessibilityControlsProps): JSX.Element | null {
// If hidden, return nothing
if (hideAccessibilityControls) return null;

Expand Down
11 changes: 5 additions & 6 deletions packages/react/src/LazyVideo/LazyVideoClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { useInView } from 'react-intersection-observer'
import { useMediaQueries } from '@react-hook/media-query'
import { useEffect, type ReactElement, useRef, useCallback, type MutableRefObject, useState } from 'react'
import { useEffect, useRef, useCallback, type MutableRefObject, useState } from 'react'
import type { LazyVideoProps } from '../types/lazyVideoTypes';
import { fillStyles, transparentGif } from '../lib/styles'
import AccessibilityControls from './AccessibilityControls'
Expand Down Expand Up @@ -38,8 +38,7 @@ export default function LazyVideoClient({
pauseIcon,
hideAccessibilityControls,
accessibilityControlsPosition,
}: LazyVideoClientProps): ReactElement {

}: LazyVideoClientProps): JSX.Element {
// Track the actual video playback state. Start in a paused state because
// even with an autoplay video, it won't actually have started playing yet.
const [isVideoPaused, setVideoPaused] = useState(true)
Expand Down Expand Up @@ -173,9 +172,9 @@ export default function LazyVideoClient({

// Switch the video asset depending on media queries
function ResponsiveSource({
mediaSrcs, videoRef
}: ResponsiveVideoSourceProps): ReactElement | undefined {

mediaSrcs,
videoRef,
}: ResponsiveVideoSourceProps): JSX.Element {
// Find the src url that is currently active
const { matches } = useMediaQueries(mediaSrcs)
const srcUrl = getFirstMatch(matches)
Expand Down
10 changes: 3 additions & 7 deletions packages/react/src/LazyVideo/LazyVideoServer.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import type { ReactElement } from 'react'
import type { LazyVideoProps } from '../types/lazyVideoTypes'
import LazyVideoClient from './LazyVideoClient'

// This wrapper function exists to take Function props and make them
// serializable for the LazyVideoClient component, which is a Next.js style
// client component.
export default function LazyVideo(
props: LazyVideoProps
): ReactElement | undefined {

export default function LazyVideo(props: LazyVideoProps): JSX.Element | null {
// Destructure some props
const {
src,
Expand All @@ -32,7 +28,7 @@ export default function LazyVideo(
return [url, media]
})
// If the array ended up empty, abort
if (mediaSrcEntries.filter(([url]) => !!url).length == 0) return
if (mediaSrcEntries.filter(([url]) => !!url).length == 0) return null;

// Make the hash
mediaSrcs = Object.fromEntries(mediaSrcEntries)
Expand All @@ -41,7 +37,7 @@ export default function LazyVideo(
} else {
if (videoLoader) srcUrl = videoLoader({ src })
else if (typeof src == 'string') srcUrl = src
if (!srcUrl) return // If no url could be built, abort
if (!srcUrl) return null; // If no url could be built, abort
}

// Render client component
Expand Down
21 changes: 10 additions & 11 deletions packages/react/src/PictureImage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ReactElement } from 'react'
import type { PictureImageProps } from './types/pictureImageTypes'
import type { ImageLoader, SourceMedia, SourceType } from './types/reactVisualTypes'
import { deviceSizes, imageSizes } from './lib/sizes'
Expand All @@ -14,10 +13,7 @@ type ImageSourceProps = {
media?: SourceMedia
}

export default function PictureImage(
props: PictureImageProps
): ReactElement {

export default function PictureImage(props: PictureImageProps): JSX.Element {
// Destructure props
const {
src,
Expand Down Expand Up @@ -94,12 +90,15 @@ function makeSrcUrl(

// Make a source tag with srcset for the provided type and/or media attribute
function Source({
widths, imageLoader, sizes, src, type, media
}: ImageSourceProps): ReactElement {
const srcSet = makeSrcSet(widths, imageLoader, { src, type, media })
return (
<source {...{ type, media, srcSet, sizes }} />
)
widths,
imageLoader,
sizes,
src,
type,
media,
}: ImageSourceProps): JSX.Element {
const srcSet = makeSrcSet(widths, imageLoader, { src, type, media });
return <source {...{ type, media, srcSet, sizes }} />;
}

// Make a srcset string from an array of width integers using the imageLoader
Expand Down
4 changes: 1 addition & 3 deletions packages/react/src/ReactVisual.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { ReactElement } from 'react'

import VisualWrapper from './VisualWrapper'
import LazyVideo from './LazyVideo'
import PictureImage from './PictureImage'
Expand All @@ -10,7 +8,7 @@ import { fillStyles } from './lib/styles'

export default function ReactVisual(
props: ReactVisualProps
): ReactElement | null {
): JSX.Element | null {

// Destructure props
const {
Expand Down
28 changes: 19 additions & 9 deletions packages/react/src/VisualWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CSSProperties, ReactElement } from 'react'
import type { CSSProperties } from 'react'
import { fillStyles, cx } from './lib/styles'
import { isNumeric } from './lib/values'
import type { VisualWrapperProps } from './types/visualWrapperTypes'
Expand All @@ -13,11 +13,18 @@ type MakeResponsiveAspectsProps = Pick<VisualWrapperProps,

// Wraps media elements and applys layout and other functionality
export default function VisualWrapper({
expand, width, height,
aspect, sourceMedia, image, video,
children, className, style, dataAttributes
}: VisualWrapperProps): ReactElement {

expand,
width,
height,
aspect,
sourceMedia,
image,
video,
children,
className,
style,
dataAttributes,
}: VisualWrapperProps): JSX.Element {
// If aspect is a function, invoke it to determine the aspect ratio
let aspectRatio, aspectStyleTag, aspectClasses
if (typeof aspect == 'function' && sourceMedia && sourceMedia.length) {
Expand Down Expand Up @@ -51,10 +58,13 @@ export default function VisualWrapper({

// Create a style tag that applies responsive aspect ratio values
function makeResponsiveAspects({
aspectCalculator, sourceMedia, image, video
aspectCalculator,
sourceMedia,
image,
video,
}: MakeResponsiveAspectsProps): {
aspectClasses: string
aspectStyleTag: ReactElement
aspectClasses: string;
aspectStyleTag: JSX.Element;
} {

// Make CSS classes and related rules that are specific to the query and
Expand Down
3 changes: 1 addition & 2 deletions packages/sanity-next/src/SanityNextVisual.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ReactElement } from 'react'
import NextVisual from '@react-visual/next'
import { makeImageUrl, makeFileUrl, makeImageLoader } from './lib/urlBuilding'
import {
Expand All @@ -11,7 +10,7 @@ import { SanityNextVisualProps } from './types/sanityNextVisualTypes'

export default function SanityNextVisual(
props: SanityNextVisualProps
): ReactElement | null {
): JSX.Element | null {

// Destructure some props
let {
Expand Down

0 comments on commit 79a70a1

Please sign in to comment.