Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Calendar] [theme] Calendar theme stylesheet customization isn't working as expected #2336

Open
badalsaibo opened this issue Oct 16, 2023 · 6 comments · May be fixed by #2573
Open

[Calendar] [theme] Calendar theme stylesheet customization isn't working as expected #2336

badalsaibo opened this issue Oct 16, 2023 · 6 comments · May be fixed by #2573
Labels

Comments

@badalsaibo
Copy link

badalsaibo commented Oct 16, 2023

Description

Calendar theme stylesheet customization isn't working as expected. The theme props accepts the stylesheet object of the following structure as derived from its types

stylesheet?: {
        calendar?: {
            main?: object;
            header?: object;
        };
        day?: {
            basic?: object;
            period?: object;
        };
        dot?: object;
        marking?: object;
        'calendar-list'?: {
            main?: object;
        };
        agenda?: {
            main?: object;
            list?: object;
        };
        expandable?: {
            main?: object;
        };
 };

Hence, adding the following object to the theme prop should work

stylesheet: {
    calendar: {
        main: {
            week: {
                justifyContent: 'space-between',
                backgroundColor: 'blue',
            },
            monthView: {
                backgroundColor: 'red',
            },
            dayContainer: {
                alignItems: 'center',
                backgroundColor: 'blue',
            },
        },
    },
},

Expected Behavior

The theming should be applied.

Observed Behavior

The theming isn't applied.

Why is this happening

If you look at node_modules/react-native-calendars/src/calendar/style.js you'll observe that getStyle function has

...(theme['stylesheet.calendar.main'] || {})

this should be changed to

...(theme.stylesheet.calendar.main || {})
@ApacheEx
Copy link

+1 having the same issue, or we fix how we get the theme value, or types for Theme interface.

@badalsaibo
Copy link
Author

For now as a workaround one can override the styles by directly setting the theme on stylesheet.calendar.main property like this

<Calendar
  theme={{
    'stylesheet.calendar.main': {
      week: {
        justifyContent: 'space-between',
        flexDirection: 'row',
      },
      monthView: {
        padding: 0,
        margin: 0,
      },
      dayContainer: {
        alignItems: 'center',
        flex: 1,
      },
      container: {
        padding: 0,
      },
    },
  }}
/>

@fabiendeborde
Copy link

I was updating an old project and had to update this library.
The following theme keys were giving me Typescript error:

  • 'stylesheet.calendar.header'
  • 'stylesheet.calendar.main'

but they are still working, as stated above by @badalsaibo

I just had to add a comment above my code to prevent TS errors:
// @ts-ignore: types / theme handling bug

I have no idea if in the newer version, the documentation is right or the types are, so I am going to finish updating this project as quickly as I can, so I don't enter a "dependencies update hell"...

@bartvanandel
Copy link

bartvanandel commented Dec 18, 2023

If you look at node_modules/react-native-calendars/src/calendar/style.js you'll observe that getStyle function has

...(theme['stylesheet.calendar.main'] || {})

this should be changed to

...(theme.stylesheet.calendar.main || {})

I think this is only half the solution For example, <ExpandableCalendar/> already uses ...(theme?.stylesheet?.expandable?.main || {}), however this overwrites the entire style of each provided component instead of simply augmenting it.

So, I think in all styles.js files, this call:

return StyleSheet.create({
  // default styles,
  ...(theme?.stylesheet?.xxx?.yyy || {}) // or worse, ...(theme['stylesheet.xxx.yyy'])
})

should ideally be changed into this:

return StyleSheet.flatten(
  {
    // default styles,
  }, 
  theme?.stylesheet?.xxx?.yyy ?? {}
})

Copy link

stale bot commented Feb 1, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Feb 1, 2025
@danielbogomazov danielbogomazov linked a pull request Mar 19, 2025 that will close this issue
@danielbogomazov
Copy link

danielbogomazov commented Mar 19, 2025

Hey everyone, I’ve set up a PR to address this issue: #2573.

If you’d like to use the fix before it’s merged, you can apply it using patch-package.

A quick note: overriding specific styles using the stylesheet prop in the Theme will replace the entire style object. I recommend identifying the specific style you want to update, copying over all the existing styles, and then applying your custom changes.

This issue could potentially be resolved by implementing something similar to bartvanandel's suggestion of using StyleSheet.flatten instead of StyleSheet.create. However, I left that out of my PR because it might introduce hard-to-track side effects in existing codebases (and should probably have its own PR anyways). For example, if someone designed their styles with the current override behavior in mind, switching to a merge-based approach could cause unexpected issues. A different strategy might be needed to ensure backward compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants