Skip to content

no-unused-styles: false positive with variable based style object selection #320

Open
@soullivaneuh

Description

@soullivaneuh

Considered this component having a variant prop with values white and primary, affecting the selection of the styles object:

import React, { FC } from 'react';
import {
  StyleSheet, KeyboardAvoidingView, Platform, View,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { colors } from '@company/design';
import Constants from 'expo-constants';

interface Props {
  logonScreen?: boolean;
  variant?: 'white' | 'primary';
}

const styles = StyleSheet.create({
  safeArea: {
    flex: 1,
    backgroundColor: colors.white,
  },
  safeAreaLogon: {
    backgroundColor: 'white',
  },
  container: {
    flex: 1,
  },
  center: {
    justifyContent: 'center',
  },
});

const variants = {
  white: StyleSheet.create({
    container: {
      backgroundColor: 'white',
    },
  }),
  primary: StyleSheet.create({
    container: {
      backgroundColor: colors.primary,
    },
  }),
};

export const ScreenWrapper: FC<Props> = ({
  logonScreen, children, variant,
}) => {
  const variantStyle = variants[variant || 'white'];
  const viewStyle = [
    styles.container,
    variantStyle.container,
    ...logonScreen ? [styles.center] : [],
  ];

  return (
    <SafeAreaView
      style={[
        styles.safeArea,
        ...logonScreen ? [styles.safeAreaLogon] : [],
      ]}
    >
      <View style={viewStyle}>
        {
          Platform.OS === 'ios'
            ? (
              <KeyboardAvoidingView
                style={{ flex: 1 }}
                behavior="padding"
                keyboardVerticalOffset={Constants.statusBarHeight}
              >
                {children}
              </KeyboardAvoidingView>
            )
            : children
        }
      </View>
    </SafeAreaView>
  );
};

export default null;

ESlint will complain with the following:

error  Unused style detected: undefined.container  react-native/no-unused-styles

Maybe related to #125.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions