|
| 1 | +const { withAndroidStyles } = require('expo/config-plugins'); |
| 2 | + |
| 3 | +const THEME_PRESET = { |
| 4 | + // DayNight + Bridge is the default because it is more robust with Expo edge-to-edge. |
| 5 | + edgeToEdge: '@style/NutrientRN.AppTheme', |
| 6 | + // Full Nutrient app theme for apps that want the SDK's complete default styling. |
| 7 | + fullNutrient: '@style/PSPDFKit.Theme.Default', |
| 8 | +}; |
| 9 | + |
| 10 | +function resolveThemeParent(props = {}) { |
| 11 | + if (typeof props.androidAppThemeParent === 'string' && props.androidAppThemeParent.length > 0) { |
| 12 | + return props.androidAppThemeParent; |
| 13 | + } |
| 14 | + |
| 15 | + if (props.androidThemePreset && THEME_PRESET[props.androidThemePreset]) { |
| 16 | + return THEME_PRESET[props.androidThemePreset]; |
| 17 | + } |
| 18 | + |
| 19 | + return THEME_PRESET.edgeToEdge; |
| 20 | +} |
| 21 | + |
| 22 | +function setAppThemeParent(styles, parent) { |
| 23 | + const styleList = styles?.resources?.style; |
| 24 | + if (!Array.isArray(styleList)) { |
| 25 | + console.warn( |
| 26 | + '[@nutrient-sdk/react-native] Could not find <resources><style> in Android styles.xml; AppTheme parent was not modified.' |
| 27 | + ); |
| 28 | + return styles; |
| 29 | + } |
| 30 | + |
| 31 | + const appTheme = styleList.find((style) => style?.$?.name === 'AppTheme'); |
| 32 | + if (!appTheme || !appTheme.$) { |
| 33 | + console.warn( |
| 34 | + '[@nutrient-sdk/react-native] No <style name="AppTheme"> found in Android styles.xml; theme fix was not applied. Set `androidAppThemeParent` explicitly if your app uses a different theme name.' |
| 35 | + ); |
| 36 | + return styles; |
| 37 | + } |
| 38 | + |
| 39 | + appTheme.$.parent = parent; |
| 40 | + return styles; |
| 41 | +} |
| 42 | + |
| 43 | +const withNutrientAndroidTheme = (config, props = {}) => { |
| 44 | + const themeParent = resolveThemeParent(props); |
| 45 | + |
| 46 | + return withAndroidStyles(config, (config) => { |
| 47 | + config.modResults = setAppThemeParent(config.modResults, themeParent); |
| 48 | + return config; |
| 49 | + }); |
| 50 | +}; |
| 51 | + |
| 52 | +module.exports = withNutrientAndroidTheme; |
0 commit comments