Skip to content

Commit

Permalink
Merge pull request #377 from webbeetechnologies/improvements/chips
Browse files Browse the repository at this point in the history
fix: background context not available in mobile
  • Loading branch information
shashank1010 authored Feb 4, 2025
2 parents 154102d + 2c791c1 commit de237d2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bambooapp/bamboo-molecules",
"version": "0.4.4",
"version": "0.4.5",
"repository": {
"type": "git",
"url": "git+https://github.com/webbeetechnologies/bamboo-molecules.git"
Expand Down
30 changes: 30 additions & 0 deletions src/components/Surface/BackgroundContextWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ReactNode, useContext, useMemo } from 'react';
import { BackgroundContext } from '../../utils';
import { useContrastColor } from '../../hooks';

export const BackgroundContextWrapper = ({
backgroundColor,
children,
}: {
backgroundColor: string;
children: ReactNode;
}) => {
const contrastColor = useContrastColor(backgroundColor, undefined, undefined);

const parentContext = useContext(BackgroundContext);
const isTransparent = backgroundColor === 'transparent';

const surfaceContextValue = useMemo(
() => ({
backgroundColor,
color: contrastColor,
}),
[backgroundColor, contrastColor],
);

return (
<BackgroundContext.Provider value={isTransparent ? parentContext : surfaceContextValue}>
{children}
</BackgroundContext.Provider>
);
};
9 changes: 6 additions & 3 deletions src/components/Surface/Surface.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isAnimatedValue } from '../../styles/overlay';
import { inputRange } from '../../styles/shadow';
import type { MD3Elevation } from '../../core/theme/types';
import { getElevationAndroid } from './utils';
import { BackgroundContextWrapper } from './BackgroundContextWrapper';

export type Props = ComponentPropsWithRef<typeof View> & {
/**
Expand Down Expand Up @@ -109,9 +110,11 @@ const Surface = ({ elevation = 1, style, children, testID, ...props }: Props) =>
}, [backgroundColor, elevation, surfaceStyles]);

return (
<Animated.View {...props} testID={testID} style={memoizedStyles}>
{children}
</Animated.View>
<BackgroundContextWrapper backgroundColor={surfaceStyles.backgroundColor}>
<Animated.View {...props} testID={testID} style={memoizedStyles}>
{children}
</Animated.View>
</BackgroundContextWrapper>
);
};

Expand Down
25 changes: 15 additions & 10 deletions src/components/Surface/Surface.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isAnimatedValue } from '../../styles/overlay';
import { inputRange } from '../../styles/shadow';
import type { MD3Elevation } from '../../core/theme/types';
import { getStyleForAnimatedShadowLayer, getStyleForShadowLayer } from './utils';
import { BackgroundContextWrapper } from './BackgroundContextWrapper';

export type Props = ComponentPropsWithRef<typeof View> & {
/**
Expand Down Expand Up @@ -106,24 +107,28 @@ const Surface = ({ elevation = 1, style, children, testID, ...props }: Props) =>

if (isAnimatedValue(elevation)) {
return (
<Animated.View style={layer0Style}>
<Animated.View style={layer1Style}>
<Animated.View {...props} testID={testID} style={sharedStyle}>
{children}
<BackgroundContextWrapper backgroundColor={surfaceStyles.backgroundColor}>
<Animated.View style={layer0Style}>
<Animated.View style={layer1Style}>
<Animated.View {...props} testID={testID} style={sharedStyle}>
{children}
</Animated.View>
</Animated.View>
</Animated.View>
</Animated.View>
</BackgroundContextWrapper>
);
}

return (
<Animated.View style={layer0Style}>
<Animated.View style={layer1Style}>
<Animated.View {...props} testID={testID} style={sharedStyle}>
{children}
<BackgroundContextWrapper backgroundColor={surfaceStyles.backgroundColor}>
<Animated.View style={layer0Style}>
<Animated.View style={layer1Style}>
<Animated.View {...props} testID={testID} style={sharedStyle}>
{children}
</Animated.View>
</Animated.View>
</Animated.View>
</Animated.View>
</BackgroundContextWrapper>
);
};

Expand Down
22 changes: 7 additions & 15 deletions src/components/Surface/Surface.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ComponentPropsWithRef, ReactNode, memo, useMemo, forwardRef, useContext } from 'react';
import { ComponentPropsWithRef, ReactNode, memo, useMemo, forwardRef } from 'react';
import { Animated, View, StyleProp, ViewStyle } from 'react-native';

import type { MD3Elevation } from '../../core/theme/types';
import { useComponentStyles, useContrastColor } from '../../hooks';
import { useComponentStyles } from '../../hooks';
import shadow from '../../styles/shadow';
import { BackgroundContext } from '../../utils';
import { BackgroundContextWrapper } from './BackgroundContextWrapper';

export type Props = ComponentPropsWithRef<typeof View> & {
/**
Expand Down Expand Up @@ -73,27 +73,19 @@ export type Props = ComponentPropsWithRef<typeof View> & {
// for Web
const Surface = ({ elevation = 1, style, children, testID, ...props }: Props, ref: any) => {
const surfaceStyles = useComponentStyles('Surface', style);
const contrastColor = useContrastColor(surfaceStyles?.backgroundColor, undefined, undefined);

const parentContext = useContext(BackgroundContext);
const isTransparent = surfaceStyles?.backgroundColor === 'transparent';

const { surfaceStyle, surfaceContextValue } = useMemo(() => {
const { surfaceStyle } = useMemo(() => {
return {
surfaceContextValue: {
backgroundColor: surfaceStyles?.backgroundColor,
color: contrastColor,
},
surfaceStyle: [elevation ? shadow(elevation) : null, surfaceStyles],
};
}, [elevation, surfaceStyles, contrastColor]);
}, [elevation, surfaceStyles]);

return (
<BackgroundContext.Provider value={isTransparent ? parentContext : surfaceContextValue}>
<BackgroundContextWrapper backgroundColor={surfaceStyles.backgroundColor}>
<Animated.View ref={ref} {...props} testID={testID} style={surfaceStyle}>
{children}
</Animated.View>
</BackgroundContext.Provider>
</BackgroundContextWrapper>
);
};

Expand Down
1 change: 1 addition & 0 deletions src/components/Surface/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as Surface, Props as SurfaceProps } from './Surface';
export { defaultStyles as surfaceStyles } from './utils';
export * from './BackgroundContextWrapper';

0 comments on commit de237d2

Please sign in to comment.