Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 1x 18x 18x 18x 18x 18x 18x 18x 18x 10x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 4x 4x 18x 3x 15x 1x 1x 1x | import React, { useEffect, useState } from 'react';
import { Image, Platform, Pressable, StyleSheet, Text, View, ViewStyle } from 'react-native';
import type { BannerNativeProps, BannerTextPosition } from '../Banner.types';
import { useTheme } from '@sb/ui/components/Themes/ThemeProvider';
import { colors } from '@sb/styles/colors';
export const Banner: React.FC<BannerNativeProps> = ({
id,
style,
alt,
image,
imageFit = 'cover',
imageStyle,
ref,
heading,
subheading,
textPosition = 'center',
onPressEffect = false,
shadowOnPress = false,
innerShadowOnPress = false,
layerBlurOnPress = false,
dimensions = 'medium',
disableDefaultDimensions = false,
aspectRatio,
autoAspectRatio = false,
fallbackHeight = 200,
}) => {
const [pressed, setPressed] = useState(false);
const [derivedAspectRatio, setDerivedAspectRatio] = useState<number | undefined>(aspectRatio);
const theme = useTheme();
const themeColors = colors[theme.theme];
const shadowContainerStyle = [
pressed &&
shadowOnPress &&
Platform.select({
ios: {
shadowColor: themeColors.shadowMedium,
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.25,
shadowRadius: 8,
paddingHorizontal: 4,
paddingVertical: 4,
},
android: {
elevation: 8,
paddingHorizontal: 4,
paddingVertical: 4,
},
default: {
shadowColor: themeColors.shadowMedium,
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.25,
shadowRadius: 8,
paddingHorizontal: 4,
paddingVertical: 4,
},
}),
];
const flattened = StyleSheet.flatten(style) || {};
const explicitSize = flattened.width !== undefined && flattened.height !== undefined;
useEffect(() => {
Eif (derivedAspectRatio || !autoAspectRatio || !image) return;
let cancelled = false;
try {
if (typeof image === 'number') {
const source = Image.resolveAssetSource(image);
if (source?.width && source?.height && !cancelled) {
setDerivedAspectRatio(source.width / source.height);
}
} else if (typeof image === 'string') {
Image.getSize(
image,
(w, h) => {
if (!cancelled && w && h) setDerivedAspectRatio(w / h);
},
() => {}
);
}
} catch {
// do nothing
}
return () => {
cancelled = true;
};
}, [image, autoAspectRatio, derivedAspectRatio]);
const containerStyle: ViewStyle[] = [styles.banner];
Eif (!disableDefaultDimensions && !explicitSize) {
containerStyle.push(dimensionsStyles[dimensions]);
}
Eif (!explicitSize) {
Iif (derivedAspectRatio || aspectRatio) {
containerStyle.push({
aspectRatio: (derivedAspectRatio || aspectRatio) as number,
width: '100%',
});
} else Iif (autoAspectRatio) {
containerStyle.push({ width: '100%', height: fallbackHeight });
}
}
Eif (style) {
const flattenedStyle = StyleSheet.flatten(style) as ViewStyle;
containerStyle.push(flattenedStyle);
}
if (pressed && onPressEffect) containerStyle.push(styles.scaleDown);
const textStyle: ViewStyle[] = [styles.textContainer, textPositionStyles[textPosition]];
const BannerContent = (
<Pressable
id={id}
style={containerStyle}
testID={id}
onPressIn={() => setPressed(true)}
onPressOut={() => setPressed(false)}
ref={ref}
accessibilityLabel={alt}
>
<>
{image && (
<Image
testID={`${id}-image`}
source={typeof image === 'string' ? { uri: image } : image}
resizeMode={imageFit}
alt={alt ?? 'Banner image'}
blurRadius={pressed && layerBlurOnPress ? 10 : 0}
style={[StyleSheet.absoluteFillObject, styles.bannerLayer, imageStyle]}
/>
)}
{pressed && innerShadowOnPress && (
<Pressable
testID={`${id}-inner-shadow-overlay`}
style={[styles.InnerShadowOverlay, { backgroundColor: themeColors.shadowLight }]}
pointerEvents="none"
/>
)}
<View style={textStyle} testID={`${id}-text-container`}>
{!!heading && (
<Text style={[styles.heading, { color: themeColors.colorPrimary }]}>{heading}</Text>
)}
{!!subheading && (
<Text style={[styles.subheading, { color: themeColors.colorPrimary }]}>
{subheading}
</Text>
)}
</View>
</>
</Pressable>
);
if (shadowOnPress) {
return (
<View style={shadowContainerStyle} testID={`${id}-shadow-container`}>
{BannerContent}
</View>
);
}
return BannerContent;
};
const styles = StyleSheet.create({
banner: {
position: 'relative',
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden',
borderRadius: 12,
},
bannerLayer: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
},
scaleDown: {
transform: [{ scale: 0.97 }],
},
textContainer: {
position: 'absolute',
padding: 10,
zIndex: 1,
},
heading: {
fontSize: 18,
fontWeight: 'bold',
},
subheading: {
fontSize: 14,
},
InnerShadowOverlay: {
...StyleSheet.absoluteFillObject,
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
borderRadius: 12,
zIndex: 2,
},
});
const textPositionStyles: Record<BannerTextPosition, ViewStyle> = {
'top-left': { top: 10, left: 10 },
'top-right': { top: 10, right: 10 },
'bottom-left': { bottom: 10, left: 10 },
'bottom-right': { bottom: 10, right: 10 },
center: {
justifyContent: 'center',
alignItems: 'center',
},
};
const dimensionsStyles: Record<NonNullable<BannerNativeProps['dimensions']>, ViewStyle> = {
small: { width: 160, height: 100 },
medium: { width: 240, height: 150 },
large: { width: 320, height: 200 },
};
|