Skip to content
Merged
Changes from 2 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
81 changes: 52 additions & 29 deletions packages/components/src/spectrum/View.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
/* eslint-disable react/jsx-props-no-spreading */
import { forwardRef, useMemo } from 'react';
import { CSSProperties, forwardRef, useMemo } from 'react';
import {
View as SpectrumView,
type ViewProps as SpectrumViewProps,
} from '@adobe/react-spectrum';
import type { DOMRefValue } from '@react-types/shared';
import { type ColorValue, colorValueStyle } from '../theme/colorUtils';

export type ViewProps = Omit<SpectrumViewProps<6>, 'backgroundColor'> & {
export type ViewProps = Omit<
SpectrumViewProps<6>,
| 'backgroundColor'
| 'borderColor'
| 'borderStartColor'
| 'borderEndColor'
| 'borderTopColor'
| 'borderBottomColor'
| 'borderXColor'
| 'borderYColor'
> & {
backgroundColor?: ColorValue;
borderColor?: ColorValue;
borderStartColor?: ColorValue;
Expand All @@ -21,13 +31,12 @@ export type ViewProps = Omit<SpectrumViewProps<6>, 'backgroundColor'> & {
/**
* A View component that re-exports the Spectrum View component.
* However, it overrides ColorValues to accept CSS color strings and
* our custom variable names from our color paletee and semantic colors.
* our custom variable names from our color palette and semantic colors.
*
* @param props The props for the View component
* @returns The View component
*
*/

export const View = forwardRef<DOMRefValue<HTMLElement>, ViewProps>(
(props, forwardedRef): JSX.Element => {
const {
Expand All @@ -43,34 +52,48 @@ export const View = forwardRef<DOMRefValue<HTMLElement>, ViewProps>(
...rest
} = props;

// Using shorthand to define border styling. Removing a style property during rerender can lead to styling bugs
// ex. changing from borderXColor = blue to borderColor = blue will cause the left and right borders to disappear
const defaultBorderColor = colorValueStyle(borderColor) ?? 'transparent';
const defaultBorderXColor =
colorValueStyle(borderXColor) ?? defaultBorderColor;
const defaultBorderYColor =
colorValueStyle(borderYColor) ?? defaultBorderColor;
const topColor = colorValueStyle(borderTopColor) ?? defaultBorderYColor;
const bottomColor =
colorValueStyle(borderBottomColor) ?? defaultBorderYColor;
const leftColor = colorValueStyle(borderStartColor) ?? defaultBorderXColor;
const rightColor = colorValueStyle(borderEndColor) ?? defaultBorderXColor;
const style = useMemo(() => {
const borderStyle: CSSProperties = {};
if (borderColor !== undefined) {
borderStyle.borderColor = colorValueStyle(borderColor);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be nice if we could just pass our own conversion function into View... Right now View is using useStyleProps with the viewStyleProps handlers to convert functions: https://github.com/adobe/react-spectrum/blob/bd458c1ed166a5ff1fd93f0c1a397e1540b3d880/packages/%40react-spectrum/view/src/View.tsx#L26C43-L26C57
If we could just pass our own color conversion handler in there somehow, then we wouldn't have to rewrite these props for every color: https://github.com/adobe/react-spectrum/blob/bd458c1ed166a5ff1fd93f0c1a397e1540b3d880/packages/%40react-spectrum/utils/src/styleProps.ts#L263
I'm tempted to file a ticket/PR in the Adobe GitHub repo to add this functionality, since we're ending up having to duplicate a bunch of things here just to handle the borders... but this should be fine for now.

}
if (borderXColor !== undefined) {
borderStyle.borderLeftColor = colorValueStyle(borderXColor);
borderStyle.borderRightColor = colorValueStyle(borderXColor);
}
if (borderYColor !== undefined) {
borderStyle.borderTopColor = colorValueStyle(borderYColor);
borderStyle.borderBottomColor = colorValueStyle(borderYColor);
}
if (borderStartColor !== undefined) {
borderStyle.borderLeftColor = colorValueStyle(borderStartColor);
Comment thread
ethanalvizo marked this conversation as resolved.
Outdated
}
if (borderEndColor !== undefined) {
borderStyle.borderRightColor = colorValueStyle(borderEndColor);
}
if (borderTopColor !== undefined) {
borderStyle.borderTopColor = colorValueStyle(borderTopColor);
}
if (borderBottomColor !== undefined) {
borderStyle.borderBottomColor = colorValueStyle(borderBottomColor);
}

const style = useMemo(
() => ({
return {
...UNSAFE_style,
backgroundColor: colorValueStyle(backgroundColor),
borderColor: `${topColor} ${rightColor} ${bottomColor} ${leftColor}`,
}),
[
backgroundColor,
UNSAFE_style,
topColor,
rightColor,
bottomColor,
leftColor,
]
);
...borderStyle,
};
}, [
backgroundColor,
UNSAFE_style,
borderColor,
borderStartColor,
borderEndColor,
borderTopColor,
borderBottomColor,
borderXColor,
borderYColor,
]);

return <SpectrumView {...rest} ref={forwardedRef} UNSAFE_style={style} />;
}
Expand Down