This article was started before Automattic's acquisition of Frontity and its team. Frontity's founders plan to transition the framework into a community-driven project, ensuring its stability, bug-free operation, and comprehensive documentation. Like other open-source projects, Frontity will remain free, offering opportunities for community contributions and enhancements. Further details are available in their FAQ.
My previous article detailed creating a headless WordPress site with Frontity and briefly examined its file structure. This article delves into the @frontity/mars-theme
package (Mars Theme), providing a step-by-step guide to customization. The Mars Theme serves as an excellent starting point, being Frontity's default theme, similar to WordPress's Twenty Twenty-One.
We'll explore the core components of the Mars Theme, including its "building blocks," and examine the various components included in the package. We'll cover functionality, operation, and styling with practical examples.
Let's begin!
Let's revisit the Frontity project file structure from the previous article, highlighting Frontity's core components: frontity.settings.js
, package.json
, and the packages/mars-theme
folder. The package.json
file provides crucial project information, including name, description, author, and dependencies. Key packages include:
frontity
: The main package containing Frontity app development methods and the CLI.@frontity/core
: Handles bundling, rendering, merging, transpiling, and serving. Direct access isn't typically needed for app development. A complete list is in the Frontity documentation.@frontity/wp-source
: Connects to the WordPress REST API, fetching data for the Mars Theme.@frontity/tiny-router
: Manages window.history
and routing.@frontity/html2react
: Converts HTML to React, using processors to replace HTML sections with React components.Frontity's core, or @frontity/package
(also called a "building block"), includes useful React component libraries in @frontity/components
, exporting components like Link
, Auto Prefetch
, Image
, Props
, Iframe
, Switch
, and other functions and objects. Detailed descriptions and syntax information are in the package reference API.
The Frontity documentation explains the project startup process: All packages defined in frontity.settings.js
are imported by @frontity/file-settings
, and their settings and exports are merged by @frontity/core
into a single store. This store allows access to the state and actions of different packages during development using @frontity/connect
, Frontity's state manager.
Next, we'll examine how these building blocks are used within the Mars Theme to create a functional Frontity project with a headless WordPress endpoint.
Before customization, let's familiarize ourselves with the Mars Theme (@frontity/mars-theme
) file structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
The Mars Theme's key components are: /src/index.js
, src/list/index.js
, and src/components/index.js
. The Frontity documentation provides detailed explanations of how these components are defined and interconnected. Let's focus on the three most important: Root, Theme, and List.
/src/index.js
)The src/index.js
file (the theme's Root) is crucial. It serves as the entry point, targeting a <div> in the site markup to inject the roots of all installed packages. A Frontity theme exports a <code>root
and other necessary packages into the DOM. The Frontity documentation illustrates this using Slot and Fill extensibility patterns. An example from the Mars Theme package shows how it initializes the Root component:
1 2 |
|
The Root component exports packages including roots
, fills
, state
, actions
, and libraries
. More details on the Root component are in the Frontity documentation.
/src/components/index.js
)The Frontity Theme component is the main root-level component exported by the Theme
namespace. It's wrapped with the @frontity/connect
function, providing access to state
, actions
, and libraries
props. This allows the Theme component to read the state, use actions, and utilize code from other packages.
1 2 |
|
This example, from the Mars Theme's /src/components/index.js
, uses state.source.get()
to retrieve data for rendering components like List and Post.
The previous section covered theme-level components. Now, let's examine a specific component: List.
The List component is exported by src/components/list/index.js
, using @loadable/components
for code splitting. The component loads only when a user views a list; it doesn't render when viewing a single post.
The src/components/list/list.js
component renders lists of posts using state.source.get(link)
and its items
field.
1 2 |
|
The connect
function provides access to the global state. list-item.js
and pagination.js
are also imported.
The Pagination
component (src/components/list/pagination.js
) allows users to navigate between pages of posts.
1 2 |
|
The connect
function grants access to the global state and actions.
The Post
component displays single posts and pages. The structure is similar, except posts include metadata (author, date, categories, etc.).
1 2 |
|
Conditional rendering ensures metadata is displayed only for posts, and featured images are shown based on theme settings.
The List component is crucial for displaying posts. Let's examine other essential components.
src/components/link.js
)The MarsLink
component (src/components/link.js
) is a wrapper around the @frontity/components/link
component.
1 2 |
|
It includes a handler to close the mobile menu when a link is clicked.
src/components/nav.js
)The Nav
component (src/components/nav.js
) iterates over menu items defined in frontity.settings.js
or the Frontity state, matching URLs and displaying components within the Header.
1 2 |
|
The connect
function provides access to the state. Additional menu components (menu.js
and menu-modal.js
) are provided for mobile views.
/src/components/featured-media.js
)Featured media is defined in the Root component's theme.state.featured
. The full code is in /src/components/featured-media.js
.
Styling in Frontity differs from WordPress. Frontity provides reusable components built with styled-components and Emotion, a CSS-in-JS library.
Styled-components are created using Emotion's styled
function. For example:
1 2 3 4 5 6 7 8 9 |
|
The css
prop allows inline styling using template literals.
1 2 3 4 5 6 7 8 |
|
<global></global>
componentThe <global></global>
component applies site-wide styles.
1 |
|
This section demonstrates customizing the Mars Theme. (Detailed examples and code snippets omitted for brevity; refer to the original response for complete code examples.) Key customizations include:
<global></global>
style components.(List of resources and credits omitted for brevity; refer to the original response for the complete list.)
This exploration of Frontity and its Mars Theme highlights its beginner-friendliness, low maintenance, and compatibility with experimental block themes. However, the current hosting costs and areas for documentation improvement are noted. Further exploration of headless site development using Gatsby and Frontity, along with the evolution of WordPress block themes, is planned.
The above is the detailed content of Mars Theme: A Deep Look at Frontity's Headless WordPress Theme. For more information, please follow other related articles on the PHP Chinese website!