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

[FREQ] Problems with using @Environment(\.colorScheme) var colorScheme #148

Open
Kejiasir opened this issue Oct 18, 2024 · 5 comments
Open
Assignees
Labels

Comments

@Kejiasir
Copy link

When using @Environment(\.colorScheme) var colorScheme, Xcode prompts: Accessing Environment<ColorScheme>'s value outside of being installed on a View. This will always read the default value and will not update.

Sample code:

struct SamplePopupView: BottomPopup {
    
    @Environment(\.colorScheme) var colorScheme
    
    func configurePopup(popup: BottomPopupConfig) -> BottomPopupConfig {
        popup.backgroundColour(colorScheme == .light ? .black : .white)
    }
    
    func createContent() -> some View {
        // code
    }
    
}

As Xcode suggests, it is not possible to change colors based on a colorScheme.

Is there a way to fix this?

@FulcrumOne
Copy link
Contributor

FulcrumOne commented Oct 18, 2024

Hey @Kejiasir,

Thanks for sharing this issue. I don't use @Environment values, so I wasn't aware of this problem. I'll try to solve it over the weekend and get back to you with a solution (hopefully).

PS. If you have some spare time, could you confirm that the problem exists in the latest framework version (3.0.0)? Thanks for your help!

@Kejiasir
Copy link
Author

Hi @FulcrumOne, thank you for your reply, and thank you for open-sourcing this excellent framework. I am currently using version 2.7.1, so I haven't had time to confirm whether this problem exists in version 3.0.0.

@FulcrumOne
Copy link
Contributor

Hey @Kejiasir

I found a way to fix it. I'll implement it this week, till Friday probably.

@Kejiasir
Copy link
Author

Wow @FulcrumOne Great, thank you very much.

@FulcrumOne
Copy link
Contributor

FulcrumOne commented Oct 23, 2024

Hey @Kejiasir,

Quick update - this is a bit more complicated than I initially thought. The problem is that I've implemented it in such a way that the configurePopup() method runs outside of the view context. This causes the problem that when the view refreshes, the function remains intact, which I did as part of the optimisation.

I'm currently considering moving the method to a body declaration, but I need to spend some time thinking about how this will affect the code efficiency.

Another idea is to put Config as a @State value, which will allow it to be updated whenever some value changes (but again, I need to investigate how it will affect the code efficiency).

I'll try my best to finish the task this week.

Have a nice day,
Tomasz

@FulcrumOne FulcrumOne changed the title Problems with using @Environment(\.colorScheme) var colorScheme [FEATURE REQUEST] Problems with using @Environment(\.colorScheme) var colorScheme Oct 28, 2024
@FulcrumOne FulcrumOne changed the title [FEATURE REQUEST] Problems with using @Environment(\.colorScheme) var colorScheme [FREQ] Problems with using @Environment(\.colorScheme) var colorScheme Oct 28, 2024
@FulcrumOne FulcrumOne self-assigned this Oct 28, 2024
@FulcrumOne FulcrumOne moved this to Backlog in Popups Roadmap Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Backlog
Development

No branches or pull requests

2 participants