react-native-inner-shadow
Version:
react native inner shadows with linear gradient design UI
135 lines (132 loc) • 4.55 kB
JavaScript
"use strict";
import React, { memo } from 'react';
import { Pressable, View } from 'react-native';
import { Canvas, Shadow } from '@shopify/react-native-skia';
import Animated, { interpolateColor, useAnimatedReaction, useDerivedValue, withTiming } from 'react-native-reanimated';
import { CANVAS_PADDING, COMMON_STYLES, DAMPING_DURATION, DAMPING_RATIO, INITIAL_DEPTH, IS_REFLECTED_LIGHT_ENABLED } from "../constants.js";
import { isLinearProps } from "../utils.js";
import LinearGradientFill from "./ShadowLinearGradientFill.js";
import { useAnimatedOffset } from "../hooks/useAnimatedOffset.js";
import { CornerRadii } from "./CornerRadii.js";
import { useShadowProperties } from "../hooks/useShadowProperties.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const PressButton = Animated.createAnimatedComponent(Pressable);
export const UnifiedShadowToggle = /*#__PURE__*/memo(function ShadowToggle({
width: propWidth,
height: propHeight,
isActive = false,
activeColor,
duration = DAMPING_DURATION,
damping = DAMPING_RATIO,
isReflectedLightEnabled = IS_REFLECTED_LIGHT_ENABLED,
style,
children,
onLayout: propsOnLayout,
...props
}) {
const {
flatStyle,
bgColor,
shadowProps,
layout,
canRenderCanvas,
onLayout
} = useShadowProperties({
propWidth,
propHeight,
style,
propsOnLayout,
...props
});
const isLinear = isLinearProps(props);
const {
depth,
inset,
offset,
reflectedLightOffset,
blurRadius,
PressedAnimatedStyle
} = useAnimatedOffset({
offset: shadowProps.shadowOffset,
reflectedLightOffset: shadowProps.reflectedLightOffset,
blurRadius: shadowProps.shadowBlur,
damping,
duration,
onPressIn: props.onPressIn,
onPressOut: props.onPressOut
});
const animatedBackgroundColor = useDerivedValue(() => interpolateColor(depth.value, [INITIAL_DEPTH, -INITIAL_DEPTH * damping], [bgColor, activeColor || bgColor]));
useAnimatedReaction(() => isActive, next => {
depth.value = withTiming(next ? -INITIAL_DEPTH * damping : INITIAL_DEPTH, {
duration
});
}, [isActive]);
return /*#__PURE__*/_jsxs(View, {
onLayout: onLayout,
style: COMMON_STYLES.canvasContainer,
children: [canRenderCanvas ? /*#__PURE__*/_jsx(Canvas, {
style: [COMMON_STYLES.canvas, {
width: layout.width + CANVAS_PADDING * 2,
height: layout.height + CANVAS_PADDING * 2
}],
children: /*#__PURE__*/_jsxs(CornerRadii, {
width: layout.width,
height: layout.height,
style: flatStyle,
backgroundColor: animatedBackgroundColor,
children: [isLinear ? /*#__PURE__*/_jsx(LinearGradientFill, {
...props,
// from, to, colors, etc.
width: layout.width,
height: layout.height
}) : null, /*#__PURE__*/_jsx(Shadow, {
dx: offset.dx,
dy: offset.dy,
blur: blurRadius,
color: shadowProps.shadowColor,
inner: inset
}), isReflectedLightEnabled ? /*#__PURE__*/_jsx(Shadow, {
dx: reflectedLightOffset.dx,
dy: reflectedLightOffset.dy,
blur: blurRadius,
color: shadowProps.reflectedLightColor,
inner: true
}) : null]
})
}) : null, /*#__PURE__*/_jsx(PressButton, {
...props,
// eslint-disable-next-line react-native/no-inline-styles
style: [{
zIndex: 1
}, flatStyle, COMMON_STYLES.canvasWrapper],
children: /*#__PURE__*/_jsx(Animated.View, {
style: PressedAnimatedStyle,
children: children
})
})]
});
});
/**
* ShadowToggle
* ----------------
* A toggle component that casts a shadow when active.
* The shadow effect is created using the `@shopify/react-native-skia` library.
*
* @param isActive - Whether the shadow is active
* @param activeColor - The color of the shadow when active
*/
export const ShadowToggle = UnifiedShadowToggle;
/**
* LinearShadowToggle
* ----------------
* A toggle component that casts a linear gradient shadow when active.
* The shadow effect is created using the `@shopify/react-native-skia` library.
*
* @param isActive - Whether the shadow is active
* @param activeColor - The color of the shadow when active
* @param colors - The colors of the linear gradient
* @param from - The direction of the linear gradient
* @param to - The direction of the linear gradient
*/
export const LinearShadowToggle = UnifiedShadowToggle;
//# sourceMappingURL=ShadowToggle.js.map