Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: view border styling #2063

Merged
merged 8 commits into from
Jun 14, 2024
Merged
Changes from all 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
90 changes: 80 additions & 10 deletions packages/components/src/spectrum/View.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,106 @@
/* eslint-disable react/jsx-props-no-spreading */
import { forwardRef, useMemo } from 'react';
import { CSSProperties, forwardRef, useMemo } from 'react';
import {
useLocale,
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;
borderEndColor?: ColorValue;
borderTopColor?: ColorValue;
borderBottomColor?: ColorValue;
borderXColor?: ColorValue;
borderYColor?: ColorValue;
};

/**
* 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 { backgroundColor, UNSAFE_style, ...rest } = props;
const style = useMemo(
() => ({
const {
backgroundColor,
borderColor,
borderStartColor,
borderEndColor,
borderTopColor,
borderBottomColor,
borderXColor,
borderYColor,
UNSAFE_style,
...rest
} = props;

const { direction } = useLocale();
const style = useMemo(() => {
const borderStyle: CSSProperties = {};
if (borderColor !== undefined) {
borderStyle.borderColor = colorValueStyle(borderColor);
Copy link
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[
direction === 'rtl' ? 'borderRightColor' : 'borderLeftColor'
] = colorValueStyle(borderStartColor);
}
if (borderEndColor !== undefined) {
borderStyle[
direction === 'rtl' ? 'borderLeftColor' : 'borderRightColor'
] = colorValueStyle(borderEndColor);
}
if (borderTopColor !== undefined) {
borderStyle.borderTopColor = colorValueStyle(borderTopColor);
}
if (borderBottomColor !== undefined) {
borderStyle.borderBottomColor = colorValueStyle(borderBottomColor);
}

return {
...UNSAFE_style,
backgroundColor: colorValueStyle(backgroundColor),
}),
[backgroundColor, UNSAFE_style]
);
...borderStyle,
};
}, [
backgroundColor,
UNSAFE_style,
borderColor,
borderStartColor,
borderEndColor,
borderTopColor,
borderBottomColor,
borderXColor,
borderYColor,
direction,
]);

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