From 16ad2f2c10eb81fe73967acada14bb0c83222a3a Mon Sep 17 00:00:00 2001 From: Robin Pyon Date: Mon, 30 Oct 2023 17:24:28 +0000 Subject: [PATCH] refactor: use transient props in various styled components --- .../core/changeIndicators/ChangeIndicator.tsx | 1 - .../ElementWithChangeBar.styled.tsx | 74 +++++++++---------- .../changeIndicators/ElementWithChangeBar.tsx | 36 +++------ .../__workshop__/ChangeBarStory.tsx | 10 +-- .../field/diff/components/DiffTooltip.tsx | 8 +- .../ReferenceInput/ReferenceLinkCard.tsx | 11 ++- .../files/ImageInput/ImagePreview.styled.tsx | 16 ++-- .../inputs/files/ImageInput/ImagePreview.tsx | 2 +- .../header/DocumentHeaderTabs.tsx | 4 +- 9 files changed, 70 insertions(+), 92 deletions(-) diff --git a/packages/sanity/src/core/changeIndicators/ChangeIndicator.tsx b/packages/sanity/src/core/changeIndicators/ChangeIndicator.tsx index 608375325ab..1d23c7f6099 100644 --- a/packages/sanity/src/core/changeIndicators/ChangeIndicator.tsx +++ b/packages/sanity/src/core/changeIndicators/ChangeIndicator.tsx @@ -68,7 +68,6 @@ const ChangeBarWrapper = memo(function ChangeBarWrapper( return (
(({changed, disabled, isReviewChangeOpen}) => { - if (disabled) - return css` - ${ChangeBar} { - display: none; - } - ` +export const ChangeBarWrapper = styled.div( + ({$changed, $disabled, $isReviewChangeOpen}) => { + if ($disabled) + return css` + ${ChangeBar} { + display: none; + } + ` - return css` - --change-bar-offset: 2px; + return css` + --change-bar-offset: 2px; - display: flex; - position: relative; + display: flex; + position: relative; - @media (hover: hover) { - &:hover { - z-index: 10; + @media (hover: hover) { + &:hover { + z-index: 10; + } } - } - /* hide when field is not changed */ - ${!changed && - css` - ${ChangeBar} { - opacity: 0; - pointer-events: none; - } - `} + /* hide when field is not changed */ + ${!$changed && + css` + ${ChangeBar} { + opacity: 0; + pointer-events: none; + } + `} - /* hide hover effect when review changes is open */ - ${isReviewChangeOpen && - css` - ${ChangeBarButton} { - opacity: 0; - } - `} - ` -}) + /* hide hover effect when review changes is open */ + ${$isReviewChangeOpen && + css` + ${ChangeBarButton} { + opacity: 0; + } + `} + ` + }, +) export const FieldWrapper = styled.div` flex-grow: 1; diff --git a/packages/sanity/src/core/changeIndicators/ElementWithChangeBar.tsx b/packages/sanity/src/core/changeIndicators/ElementWithChangeBar.tsx index 71772b49201..f9d3acd889b 100644 --- a/packages/sanity/src/core/changeIndicators/ElementWithChangeBar.tsx +++ b/packages/sanity/src/core/changeIndicators/ElementWithChangeBar.tsx @@ -1,30 +1,25 @@ import {useLayer} from '@sanity/ui' -import React, {useCallback, useMemo, useState} from 'react' +import React, {useMemo} from 'react' import {ConnectorContext} from './ConnectorContext' import { - ChangeBarWrapper, - FieldWrapper, ChangeBar, - ChangeBarMarker, ChangeBarButton, + ChangeBarMarker, + ChangeBarWrapper, + FieldWrapper, } from './ElementWithChangeBar.styled' export function ElementWithChangeBar(props: { children: React.ReactNode disabled?: boolean - hasFocus: boolean isChanged?: boolean withHoverEffect?: boolean }) { - const {children, disabled, hasFocus, isChanged, withHoverEffect = true} = props + const {children, disabled, isChanged, withHoverEffect = true} = props - const [hover, setHover] = useState(false) const {onOpenReviewChanges, isReviewChangesOpen} = React.useContext(ConnectorContext) const {zIndex} = useLayer() - const handleMouseEnter = useCallback(() => setHover(true), []) - const handleMouseLeave = useCallback(() => setHover(false), []) - const changeBar = useMemo( () => disabled || !isChanged ? null : ( @@ -35,34 +30,21 @@ export function ElementWithChangeBar(props: { aria-label="Review changes" data-testid="change-bar__button" onClick={isReviewChangesOpen ? undefined : onOpenReviewChanges} - onMouseEnter={handleMouseEnter} - onMouseLeave={handleMouseLeave} tabIndex={-1} type="button" $withHoverEffect={withHoverEffect} /> ), - [ - disabled, - isChanged, - zIndex, - isReviewChangesOpen, - onOpenReviewChanges, - handleMouseEnter, - handleMouseLeave, - withHoverEffect, - ], + [disabled, isChanged, zIndex, isReviewChangesOpen, onOpenReviewChanges, withHoverEffect], ) return ( {children} {changeBar} diff --git a/packages/sanity/src/core/changeIndicators/__workshop__/ChangeBarStory.tsx b/packages/sanity/src/core/changeIndicators/__workshop__/ChangeBarStory.tsx index dd7826efc03..7de87170663 100644 --- a/packages/sanity/src/core/changeIndicators/__workshop__/ChangeBarStory.tsx +++ b/packages/sanity/src/core/changeIndicators/__workshop__/ChangeBarStory.tsx @@ -1,20 +1,16 @@ import {Container, Flex, TextArea} from '@sanity/ui' import {useBoolean} from '@sanity/ui-workshop' -import React, {useCallback, useState} from 'react' +import React from 'react' import {ElementWithChangeBar} from '../ElementWithChangeBar' export default function ChangeBarStory() { const isChanged = useBoolean('Changed', true, 'Props') - const [focused, setFocused] = useState(false) - - const handleBlur = useCallback(() => setFocused(false), []) - const handleFocus = useCallback(() => setFocused(true), []) return ( - -