Rewriting and Extending A Theme
Structured data explanation and testing example
We can go to a page in scandipwa.local
, Inspect it and copy the HTML. After this when going to Google schema checker we’ll be able to see info about the organization. This information is originally declared in the Header.component.js
file.
Additionally we’ll see the breadcrumbs list, multiple products and their details. This info in useful when you want to see your product readily selectable from a Google search.
Pagination implementation details
Go to scandipwa.local
woman’s section and you’ll see pagination happening while scrolling to the bottom of the page. Notice the URL changes while scrolling. After reloading the site will display only the current page, with the option to ‘Load previous’.
This is important for Google’s crawlers. This way they’ll receive only one page at a time.
Style structure (abstract folder)
The app/style
is divided in abstract and base styles. Abstract styles mean that these files contain no real selector declarations. These styles are available to any component’s style.
For example, the Breadcrumb.style.scss
file contains:
Which is included in the app/style/abstract/_media.scss
file as:
Refrain from declaring things in the abstract
files. This will lead to your declaration appearing in multiple component’s styles that had used the abstraction.
The style/base
folder contains the default styles that you’ll encounter. This folder contains styles for simple HTML elements without classes.
Mention of coding standards
Check out our docs on code standards - React, styles and BEM. These docs are made on the basis of compiling the most common code review issues on ScandiPWA.
Guide to rewriting
Let’s look at an example where we want to change the logo ‘ScandiPWA’ in scandipwa.local
.
First, find Header.component.js
to find out where the header is located in source
. In this case it’s located in app/component/Header
.
Go to your theme’s app pwa/src/app
and create a new folder called component
and in it a Header
folder. After this you can check if the application compiles successfully by running the alias dcf logs -f --tail-100 frontend
. Use the tail
flag to only see the last 100 lines of the log file.
You can read more about the ScandiPWA permanent aliases here.
To override the original header, add a Header.component.js
to your newly created folder.
It’s important to import from the component, because otherwise you might import the container or something else that you have not specifically asked for.
This is what your new header file will have to contain:
After doing this we wait for the dev server reload and we’ll be able to see our changes on scandipwa.local
. Instead of the ScandiPWA logo, the homepage now should have ‘Alfred’ as a link in place.
Overriding and extending the styles
A plain link text doesn’t look so great, so let’s change the styles as well. You can approach styling in two ways - completely overriding them or extending them.
In order to not have code duplications in the project, let’s extend the style file. The same way as previously make a Header.style.scss
file in the pwa/src/app/component/Header
folder. If your file contains the following:
You’ll see that the header in scandipwa.local
will have been completely overriden. So, we recommend to instead name the new style file Header.override.style.scss
. It is important to stick to the naming convention in order for the fallback mechanism to work. So, the style file name should be <name>.override.style.scss
.
Let’s also declare BEM properties for span inside Header.component.js
:
You can read more about BEM here.
The MyName
element then can be styled in the Header.override.style.scss
file:
The $black
is declared in _variables.scss
file. After writing out your style, don’t forget to import it in the Header.component.js
file. So, the imports will look like this:
The dot at the start of the path signifies that the Header.component.js
should look for the import in the same folder where it’s located.
If you want to remove the brand rendering from the product card find the ProductCard.component.js
and create a new folder, just like with the Header
folder. So, just the sake of repetition, you create in the following path: pwa/src/app/component/ProductCard
. In this folder create a ProductCard.component.js
file that will be meant for overriding the source
component. The skeleton of the code will be fairly similar to the previous header example.
Now we have to look where the brand is rendered in the source ProductCard
and copy that code into our new file.
This will remove the brand name from the product card. We can also introduce the product card to us:
Now after rendering you’ll see this text under every product card.
If we don’t like the look of breadcrumbs, we can try to change the style of them. Just like in all of the previous examples we need to make a new breadcrumbs folder in our app and create a style file in it. So, the folder path would be: pwa/src/app/component/Breadcrumbs
and the copy the source Breadcrumbs.style.scss
file in it for this example.
Let’s change the colors. Note, that you need to keep all of the rest of the style declarations, since the file’s name is not Breadcrumbs.override.style.scss
.
Creating a new page and adding it to the router
Create a new folder in the src/app/route
folder to create a new page. The path will look like this src/app/route/NewPage
. The NewPage
folder should contain a NewPage.component.js
file and index.js
file.
The NewPage.component.js
should contain:
Now we need to add this to NewPage/index.js
:
After this we need to add this additional route to the Router.component.js` file in the switch items section by adding:
Add the following to the imports:
Creating a container with connection to global state
To add breadcrumbs, create a NewPage.container.js
file in src/app/route/NewPage
directory.
Now let’s call it in NewPage.component.js
:
We also need to change the NewPage/index.js
and export the container in order to not have undefined props.
We can create style for the NewPage in the same manner as previously by adding a NewPage.style.scss
file to the NewPage
folder. There we need to declare the new page and style away!
Use variables in styles to be able to override the style from a parent component. Add the style to your component by adding import './NewPage.style';
.
We can also move the content around, so we need to BEM the elements in NewPage.component.js
:
Consequently in our NewPage.style.scss
we add:
Extending the Header business logic (its state)
Say we wanted to change stuff in the header and exchange the menu button for a ‘back’ button. Go to your header extension file Header.component.js
In your theme’s header folder add Header.container.js
where we’ll need to extend the source header’s container and export everything else.
Now, let’s add the new page to the admin dashboard. Go to scandipwa.local/admin
and log in. Then go to ‘Scandiweb’ - ‘Menu Manager’. Go to assigned items and add an aditional item with the title ‘New Page’. URL type is custom URL and it should be /new-page
and parent ‘Categories’.
After this go to cache management and ‘Flush Cache Storage’. After this you should be able to navigate to your page in scandipwa.local
.
Last updated