113c5c8bdSEvan Bacon// @ts-ignore: uses flow
213c5c8bdSEvan Baconimport normalizeColor from '@react-native/normalize-color';
3*435bbba8SBrent Vatneimport { ExpoConfig } from 'expo/config';
4*435bbba8SBrent Vatneimport { ConfigPlugin, InfoPlist, withInfoPlist } from 'expo/config-plugins';
513c5c8bdSEvan Bacon
613c5c8bdSEvan Bacon// Maps to the template AppDelegate.m
713c5c8bdSEvan Baconconst BACKGROUND_COLOR_KEY = 'RCTRootViewBackgroundColor';
813c5c8bdSEvan Bacon
913c5c8bdSEvan Baconconst debug = require('debug')('expo:system-ui:plugin:ios');
1013c5c8bdSEvan Bacon
1113c5c8bdSEvan Baconexport const withIosRootViewBackgroundColor: ConfigPlugin = (config) => {
1213c5c8bdSEvan Bacon  config = withInfoPlist(config, (config) => {
1313c5c8bdSEvan Bacon    config.modResults = setRootViewBackgroundColor(config, config.modResults);
1413c5c8bdSEvan Bacon    return config;
1513c5c8bdSEvan Bacon  });
1613c5c8bdSEvan Bacon  return config;
1713c5c8bdSEvan Bacon};
1813c5c8bdSEvan Bacon
1913c5c8bdSEvan Baconexport function setRootViewBackgroundColor(
2013c5c8bdSEvan Bacon  config: Pick<ExpoConfig, 'backgroundColor' | 'ios'>,
2113c5c8bdSEvan Bacon  infoPlist: InfoPlist
2213c5c8bdSEvan Bacon): InfoPlist {
2313c5c8bdSEvan Bacon  const backgroundColor = getRootViewBackgroundColor(config);
2413c5c8bdSEvan Bacon  if (!backgroundColor) {
2513c5c8bdSEvan Bacon    delete infoPlist[BACKGROUND_COLOR_KEY];
2613c5c8bdSEvan Bacon  } else {
2713c5c8bdSEvan Bacon    let color = normalizeColor(backgroundColor);
2813c5c8bdSEvan Bacon    if (!color) {
2913c5c8bdSEvan Bacon      throw new Error('Invalid background color on iOS');
3013c5c8bdSEvan Bacon    }
3113c5c8bdSEvan Bacon    color = ((color << 24) | (color >>> 8)) >>> 0;
3213c5c8bdSEvan Bacon    infoPlist[BACKGROUND_COLOR_KEY] = color;
3313c5c8bdSEvan Bacon
3413c5c8bdSEvan Bacon    debug(`Convert color: ${backgroundColor} -> ${color}`);
3513c5c8bdSEvan Bacon  }
3613c5c8bdSEvan Bacon  return infoPlist;
3713c5c8bdSEvan Bacon}
3813c5c8bdSEvan Bacon
3913c5c8bdSEvan Baconexport function getRootViewBackgroundColor(config: Pick<ExpoConfig, 'ios' | 'backgroundColor'>) {
4013c5c8bdSEvan Bacon  return config.ios?.backgroundColor || config.backgroundColor || null;
4113c5c8bdSEvan Bacon}
42