@frontity/components
API reference of `@frontity/components` package
This package is a collection of React components that have proven to be pretty useful for a Frontity project.

Table of Contents

How to use

In order to use it, you just have to import the component you want to use in your theme from @frontity/components/ and place it wherever needed. For example, if we want to use the <Image />component:
1
import Image from "@frontity/components/image";
Copied!

Components

<Link /> is a React component that you can use in your Frontity project to define links that works with the internal routing system. Under the hood, this component uses the actions.router.set(link) method from @frontity/tiny-router and creates an <a/> tag.
This component requires having state.source.url properly configured. Have a look at the guide Setting the URL of the WordPress data source to learn more about this.

Props

Name
Type
Required
Default
Description
link
string
yes
---
The URL to link to.
target
string
no
_self
The target of the anchor. Possible values: _self or _blank.
onClick
function
no
undefined
The onClick handler. Can be used to pass an optional callback that will be invoked on click.
scroll
boolean
no
true
Whether the browser should scroll up to the top upon navigating to a new page.
prefetch
boolean
no
true
Whether Frontity should automatically prefetch this link or not. The prefetching mode is controlled through state.theme.autoPrefetch.
All "unknown" props passed to the Link are passed down to an anchor </a> tag.

Usage

1
import Link from "@frontity/components/link";
2
​
3
const MyComponent = () => (
4
<Link link={linkUrl} onClick={(e) => console.log(e)}>
5
This is a link
6
</Link>
7
);
Copied!

Auto Prefetch

This component can help implementing some auto prefetching strategies. The configuration for this is stored in the state so final users can modify it in their sites using their frontity.settings.js file.
Imagine that my-awesome-theme uses this component. Then, people can set the auto prefetch setting like this:
1
const settings = {
2
// Other settings...
3
packages: [
4
{
5
name: "my-awesome-theme",
6
state: {
7
theme: {
8
autoPrefetch: "hover",
9
},
10
},
11
},
12
// Other packages...
13
],
14
};
Copied!
The possible values for state.theme.autoPrefetch are:
Value
Description
no
No auto prefetch.
hover
Prefetches links on hover.
in-view
Prefetch links currently visible in the viewport.
all
Prefetches all internal links on the page.
Using this <Link /> component is optional. You can create your own <Link /> component with your own logic.
Example of a custom <Link /> component implementation:
1
import React from "react";
2
import { connect } from "frontity";
3
​
4
const Link = ({
5
state,
6
actions,
7
link,
8
className,
9
children,
10
"aria-current": ariaCurrent,
11
}) => {
12
const onClick = (event) => {
13
// Do nothing if it's an external link
14
if (link.startsWith("http")) return;
15
​
16
event.preventDefault();
17
// Set the router to the new url.
18
actions.router.set(link);
19
​
20
// Scroll the page to the top
21
window.scrollTo(0, 0);
22
};
23
​
24
return (
25
<a
26
href={link}
27
onClick={onClick}
28
className={className}
29
aria-current={ariaCurrent}
30
>
31
{children}
32
</a>
33
);
34
};
35
​
36
export default connect(Link);
Copied!
Frontity provides a link processor. The link processor works with the <html2react> component and can automatically detect <a> tags in the page/post content and intelligently convert them into <Link> components.
If the href attribute of the <a> tag is either:
    a relative link, or
    an absolute link on the same domain as the WordPress data source
then the processor will convert the the <a> tag into a <Link> component.
The <Link> component created by the processor will be modelled on the <a> tag and will have properties consistent with its attributes - e.g. the link property of the <Link> component will be the same as the href attribute of <a> tag being replaced. The processor will also convert absolute links on the same domain to be relative links.
If the href attribute of the <a> tag is an absolute link on a different domain from the WordPress data source, i.e. it is a link to an external site, then that tag will remain as is and will not be replaced or converted.
In order for this to work the link processor must be imported into the theme and included in the list of html2react processors. This would normally be done in the root level index.js of your theme. See the @frontity/html2react documentation and this page for more info.
1
import link from "@frontity/html2react/processors/link";
Copied!
1
libraries: {
2
html2react: {
3
processors: [link],
4
},
Copied!
This link processor needs to be added to any theme that wants to uses this Client-side navigation for embedded links in the content.

Image

<Image /> is a React component that adds lazy-loading to the native WordPress images. Combined with @html2react/processors , you can add this functionality and optimize your images pretty easy.

Script

<Script /> is a React component that executes scripts tags found in content.

Props

Name
Type
Required
Description
src
string
no
URL to an external JavaScript file.
code
string
no
internal JavaScript code
id
string
no
ID for script element

Usage

External JavaScript file:
1
import Script from "@frontity/components/script";
2
​
3
const MyComponent = () => (
4
<Script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js />
5
);
Copied!
Internal JavaScript code
1
import Script from "@frontity/components/script";
2
​
3
const MyComponent = () => (
4
<Script
5
code={`
6
const body = document.querySelector('body');
7
​
8
// Triggers anytime anywhere in the body of the page is clicked
9
body.addEventListener('click', e => {
10
e.preventDefault();
11
console.log('Button Works');
12
});
13
`}
14
/>
15
);
Copied!

Iframe

<Iframe /> is a React component that implement lazy-load on iframe components. The approach taken in implementing this component is based off the edge cases in the table below.
Intersection Observer
Native Lazy
Height > 0
Output
true
true
true
Native Lazy Load
true
true
false
Intersection Observer
true
false
true
Intersection Observer
true
false
false
Intersection Observer
false
true
true
(not possible)
false
true
false
(not possible)
false
false
true
Normal Load (eager)
false
false
false
Normal Load (eager)
Native Lazy needs a height attribute. For that reason, we use the Intersection Observer when a height is not provided.

Props

Name
Type
Required
Description
title
string
yes
internal JavaScript code
src
string
no
URL to an external JavaScript file.
width
string
no
width of the iframe component
height
string
no
height of the iframe component
className
string
no
class name for the component
loading
string
no
"lazy" | "eager" | "auto" Default value: "lazy"
rootMargin
string
no
margin around root element

Usage

1
import Iframe from "@frontity/components/iframe";
2
​
3
const MyComponent = () => (
4
<Iframe
5
src="https://frontity.org"
6
title="Frontity"
7
height="500"
8
width="500"
9
/>
10
);
Copied!

Switch

The <Switch /> renders the first child component that returns true as the value of its when prop.
The last child component (which should not have a when prop) will be rendered if no other component matches the condition.
You can use it for routing to different components in your theme:
1
import Switch from "@frontity/components/switch";
2
​
3
const Theme = ({ state }) => {
4
const data = state.source.get(state.router.link);
5
​
6
return (
7
<Switch>
8
<Loading when={data.isFetching} />
9
<Home when={data.isHome} />
10
<Archive when={data.isArchive} />
11
<Post when={data.isPostType} />
12
<ErrorPage /> {/* rendered by default */}
13
</Switch>
14
);
15
};
Copied!
But also inside any other component. For example, in a <Header> component that has a different menu for the home:
1
import Switch from "@frontity/components/switch";
2
​
3
const Header = ({ state }) => {
4
const data = state.source.get(state.router.link);
5
​
6
return (
7
<Switch>
8
<MenuHome when={data.isHome} />
9
<Menu /> // rendered by default
10
</Switch>
11
);
12
};
Copied!
This component is an alternative to applying plain JavaScript logic in React:
1
const Theme = ({ state }) => {
2
const data = state.source.get(state.router.link);
3
​
4
return (
5
<>
6
{(data.isFetching && <Loading />) ||
7
(data.isHome && <Home />) ||
8
(data.isArchive && <Archive />) ||
9
(data.isPostType && <Post />) || <ErrorPage />}
10
</>
11
);
12
};
Copied!
Last modified 4mo ago