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

@Environment doesn't work on StaticLayout #205

Open
mfreiwald opened this issue Dec 29, 2024 · 0 comments
Open

@Environment doesn't work on StaticLayout #205

mfreiwald opened this issue Dec 29, 2024 · 0 comments

Comments

@mfreiwald
Copy link

I try to use the ThemeSwitch on my Home-Page. The implementation is like this:

struct ThemeSwitcher: HTML {
   @Environment(\.themes) var themes
   var body: some HTML {
        ForEach(themes) { theme in
            Button(theme.id) {
	        SwitchTheme(theme.id)
	    }
       }
   }
}

But using this in my Home-Page doesn't work, the Theme-Array in @Environment(\.theme) is empty.

struct Home: StaticLayout {
    var title = "Home"
    var body: some HTML {
        ThemeSwitcher() // No buttons visible because themes is empty
    }
}

But using the ThemeSwitch in the Layout works:

struct MyLayout: Layout {
    var id: String = "my-layout"

    @Environment(\.siteConfiguration) private var config

    var body: some HTML {
        HTMLDocument {
            HTMLHead(for: page, with: config)
            HTMLBody {
                Group(page.body)
                ThemeSwitcher() // theme buttons visible
            }

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

No branches or pull requests

1 participant