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

Simplify Ignite: Eliminate PageContext by incorporating it into EnvironmentValues #479

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

JPToroDev
Copy link
Collaborator

@JPToroDev JPToroDev commented Feb 7, 2025

PageContext is essentially the same exact object as EnvironmentStore—a singleton that creates a context for the current page being published, and infuses that context with relevant values.

This similarity gives us the opportunity to consolidate these objects and make creating layouts even simpler.

This PR is the first in a larger series, and while it contains several changes, they are all in service of simplifying this code:

struct MainLayout: Layout {
   var body: some HTML {
       HTMLDocument {
           HTMLHead(for: page)
           HTMLBody {
               NavBar()
               Section(page.body)
               Section {
                   SocialFooter()
                   IgniteFooter()
               }
           }
           .padding(.vertical, 80)
       }
   }
}

Into this code:

struct MainLayout: Layout {
   var body: some HTML {
       Body {
           NavBar()
           Section(page.body)
           Section {
               SocialFooter()
               IgniteFooter()
           }
       }
       .padding(.vertical, 80)
   }
}

Head and Body now mirror scenes in SwiftUI, and the default Head is automatically provided by the result builder, hence its omission above. Head needs to be included only if you want extra head elements like Analytics.

The superficial changes:

  • Restored HTMLHead and HTMLBody to Head and Body by reworking the associated type names that originally confused the compiler. Renamed HTMLDocument to Root.
  • Added an internal environment default property to RootHTML

The main change is adding a page property to EnvironmentValues, which we access in the initializers of Head and Body now, instead of requiring a page as an input.

The final PR of this series will thoroughly polish variable and type names. The important thing here is functionality.

JP Toro added 6 commits February 6, 2025 20:13
…plit `EnvironmentReader` functionality between the new `Layoutable` protocol and `RootElement`. Remove requirement for `HTMLHead` and `HTMLBody` to require being initialized with a page. Misc. type renaming.
@@ -5,7 +5,7 @@
// See LICENSE for license information.
//

public struct HTMLDocument: HTML {
struct Root: HTML {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the name Root. Document, perhaps? Or something else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the hesitation with Root? That'd be the most accurate HTML terminology for it and complement RootElement nicely.

@JPToroDev JPToroDev changed the title Simplify Ignite: Eliminate PageContext by incorporating it into EnvironmentStore Simplify Ignite: Eliminate PageContext by incorporating it into EnvironmentValues Feb 14, 2025
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

Successfully merging this pull request may close these issues.

2 participants