react-native-reanimated-modal
Version:
A lightweight and performant modal component. Designed for smooth animations, flexibility, and minimal footprint.
161 lines (149 loc) • 4.48 kB
JavaScript
/**
* Default values and configurations.
*/
export const DEFAULT_MODAL_ANIMATION_DURATION = 300;
export const DEFAULT_MODAL_SCALE_FACTOR = 0.8;
export const DEFAULT_MODAL_SWIPE_THRESHOLD = 100;
export const DEFAULT_MODAL_BOUNCE_OPACITY_THRESHOLD = 0.05;
export const DEFAULT_MODAL_SWIPE_DIRECTION = 'down';
/**
* Default backdrop configuration.
*/
export const DEFAULT_MODAL_BACKDROP_CONFIG = {
enabled: true,
color: 'black',
opacity: 0.7
};
export const DEFAULT_MODAL_BOUNCE_SPRING_CONFIG = {
dampingRatio: 0.5,
duration: 700
};
/**
* Default animation configurations.
*/
export const DEFAULT_MODAL_ANIMATION_CONFIGS = {
fade: {
type: 'fade',
duration: DEFAULT_MODAL_ANIMATION_DURATION
},
slide: {
type: 'slide',
duration: DEFAULT_MODAL_ANIMATION_DURATION,
direction: DEFAULT_MODAL_SWIPE_DIRECTION
},
scale: {
type: 'scale',
duration: DEFAULT_MODAL_ANIMATION_DURATION,
scaleFactor: DEFAULT_MODAL_SCALE_FACTOR
}
};
/**
* Default swipe configuration.
*/
export const DEFAULT_MODAL_SWIPE_CONFIG = {
enabled: true,
directions: [DEFAULT_MODAL_SWIPE_DIRECTION],
threshold: DEFAULT_MODAL_SWIPE_THRESHOLD,
bounceSpringConfig: DEFAULT_MODAL_BOUNCE_SPRING_CONFIG,
bounceOpacityThreshold: DEFAULT_MODAL_BOUNCE_OPACITY_THRESHOLD
};
/**
* Normalizes animation configuration by providing defaults for missing properties.
* @param config - Partial animation configuration or animation type string.
* @returns Complete animation configuration with defaults applied.
*/
export function normalizeAnimationConfig(config = {}) {
// Handle string type (legacy support)
if (typeof config === 'string') {
return DEFAULT_MODAL_ANIMATION_CONFIGS[config];
}
const type = config?.type || 'fade';
const defaultConfig = DEFAULT_MODAL_ANIMATION_CONFIGS[type];
return {
...defaultConfig,
...config
};
}
/**
* Normalizes backdrop configuration by providing defaults for missing properties.
* @param backdrop - Backdrop configuration.
* @returns Normalized backdrop information with enabled flag and config.
*/
export function normalizeBackdropConfig(backdrop = DEFAULT_MODAL_BACKDROP_CONFIG) {
// false - no backdrop
if (backdrop === false) {
return {
enabled: false,
isCustom: false,
config: {
...DEFAULT_MODAL_BACKDROP_CONFIG,
enabled: false
}
};
}
// ReactNode - custom renderer
if (backdrop && typeof backdrop === 'object' && 'type' in backdrop) {
return {
enabled: true,
isCustom: true,
config: DEFAULT_MODAL_BACKDROP_CONFIG,
customRenderer: backdrop
};
}
// object or undefined - use config with defaults
const config = {
...DEFAULT_MODAL_BACKDROP_CONFIG,
...backdrop
};
return {
enabled: config.enabled !== false,
isCustom: false,
config
};
}
/**
* Normalizes swipe configuration by providing defaults for missing properties.
* @param config - Partial swipe configuration.
* @returns Complete swipe configuration with defaults applied.
*/
export function normalizeSwipeConfig(config = {}) {
if (config === false) return {
enabled: false
};
return {
...DEFAULT_MODAL_SWIPE_CONFIG,
...config
};
}
/**
* Extracts swipe directions from swipe config or animation config fallback.
*/
export function getSwipeDirections(swipeConfig, animationConfig, fallback = DEFAULT_MODAL_SWIPE_DIRECTION) {
// If swipe config has directions, use them
if (swipeConfig.directions && swipeConfig.directions.length > 0) {
return swipeConfig.directions;
}
// Fallback to animation config for slide animations
if (animationConfig && animationConfig.type === 'slide' && animationConfig.direction) {
if (typeof animationConfig.direction === 'string') {
return [animationConfig.direction];
}
const endDirections = animationConfig.direction.end;
return Array.isArray(endDirections) ? endDirections : [endDirections];
}
return Array.isArray(fallback) ? fallback : [fallback];
}
/**
* Gets the slide-in direction from animation config.
*/
export function getSlideInDirection(animationConfig, fallback = DEFAULT_MODAL_SWIPE_DIRECTION) {
if (animationConfig.type === 'slide' && animationConfig.direction) {
if (typeof animationConfig.direction === 'string') {
return animationConfig.direction;
}
return animationConfig.direction.start;
}
return fallback;
}
//# sourceMappingURL=config.js.map
;