Skip to content

Commit 4260d2d

Browse files
committed
use namespaced for custom components
1 parent a38ac93 commit 4260d2d

File tree

14 files changed

+45
-202
lines changed

14 files changed

+45
-202
lines changed
Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,15 @@
1-
:loudspeaker: Hey there, Salesforce Commerce Cloud community!
1+
# Extension Chakra Base
22

3-
We’re excited to hear your thoughts on your developer experience with PWA Kit and the Composable Storefront generally! Your feedback is incredibly valuable in helping us guide our roadmap and improve our offering.
43

5-
:clipboard: Take our quick survey here: [Survey](https://forms.gle/bUZNxQ3QKUcrjhV18)
64

7-
Feel free to share this survey link with your colleagues, partners, or anyone who has experience with PWA Kit. Your input will help us shape the future of our development tools.
5+
## Installation
86

9-
Thank you for being a part of our community and for your continuous support! :raised_hands:
107

11-
# Description
128

13-
This is a sample PWA-Kit Application Extension. The purpose of this application extensions is to show how
14-
the Application Extensions API can be used to enhance your PWA-Kit base project.
159

16-
# Folder Structure
10+
## How it works
1711

18-
This directory contains the PWA Kit Application Extension base files and structure. It includes the following files:
19-
```
20-
├── src
21-
│ ├── setup-server.ts
22-
│ └── setup-client.ts
23-
└── dev
24-
```
2512

26-
1. `src/setup-server.ts`: The server-side setup function for the extension.
27-
2. `src/setup-client.ts`: The client-side setup function for the extension.
28-
3. `dev/`: PWA Kit App TypeScript template project used for developing the generated PWA Kit App Extension.
29-
30-
# Peer Dependencies
31-
32-
PWA-Kit Application Extensions are NPM packages at their most simplest form, and as such you can define
33-
what peer dependencies are required when using it. Because this sample application extension provides
34-
UI via a new "Sample" page, it requires that the below dependencies are installed at a minimum.
35-
36-
Depending on what features your application extensions provides it's recommended you include any third-party
37-
packages as peer dependencies so that your base application doesn't end up having multiple versions of a
38-
given package.
39-
40-
"react": "^18.2.0",
41-
"react-dom": "^18.2.0"
42-
43-
# Configuration
44-
45-
This section is optional and will depend on your application extensions implementation. If you have features
46-
that are configurable, then list those configurations here so that the PWA-Kit project implementor can configure
47-
the extension as they like.
48-
49-
```
50-
{
51-
path: 'sample-page'
52-
}
53-
```
54-
55-
# Installation
56-
57-
```
58-
> npm install @salesforce/extension-starter<br/>
59-
> Downloading npm package... <br/>
60-
> Installing extension... <br/>
61-
> Finished. <br/>
62-
> Congratulations! The Sample extension was successfully installed! Please visit https://www.npmjs.com/package/@salesforce/extension-starter for more information on how to use this extension.
63-
```
64-
65-
# Advanced Usage
66-
67-
In order to customize this Application Extension to your particular needs we suggest that you refer to the section titled
68-
"configuration", but if there is something that you want to customize that isn't configurable and cannot wait for a feature
69-
request to be fulfilled, then you can use overrides.
70-
71-
Below is a list of files that can't be overridden from within your PWA-Kit base project. Please refer to the documentation here on
72-
how to properly override extensions. Additionally it's up to the Application Extension developer as to which files can and
73-
cannot be overridden. Please refer to this documentation on how to write your first PWA-Kit Application Extension.
74-
75-
## Overridable Files
76-
77-
```
78-
/src/path/to/overridable/file.ts
79-
```
8013

8114

15+
## Configuration

packages/extension-chakra-base/src/components/sample-hoc.tsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/extension-chakra-base/src/hooks/use-theme.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import {useApplicationExtensions} from '@salesforce/pwa-kit-extension-sdk/react'
99
import {extendTheme, ThemeOverride} from '@chakra-ui/react'
1010
import {useExtensionConfig} from '../hooks/use-extension-config'
1111
import metaData from '../../extension-meta.json'
12-
import * as ChakraUI from '@chakra-ui/react'
13-
14-
const chakraBuiltInComponents = Object.keys(ChakraUI)
1512

1613
/**
1714
* This hook will look into each extension theme and merge all the themes into one single theme
@@ -30,41 +27,16 @@ export const useMergedTheme = () => {
3027
const extensionWithoutBase = extensions.filter((extension) => {
3128
return extension.getName() !== metaData.name
3229
})
33-
const componentsInExtensions = extensionWithoutBase
34-
.map((extension) => {
35-
const theme =
36-
extension.getTheme && extension.getConfig()?.applyTheme ? extension.getTheme() : {}
37-
return Object.keys(theme.components)
38-
})
39-
.flat()
40-
41-
// we only want to namespace duplicate component from different extensions that is not chakra built in components
42-
const duplicateComponents = componentsInExtensions
43-
.filter(
44-
(item, index, arr) => arr.indexOf(item) !== index && arr.lastIndexOf(item) === index
45-
)
46-
.filter((comp) => !chakraBuiltInComponents.includes(comp))
47-
console.log('duplicateComponents', duplicateComponents)
4830

4931
// TODO: fix types
5032
const extensionThemes: ThemeOverride[] = extensionWithoutBase
5133
.map((extension) => {
5234
const extensionTheme =
5335
extension.getTheme && extension.getConfig()?.applyTheme ? extension.getTheme() : {}
54-
// Should we get rid of origin component name after name spacing it?
55-
Object.keys(extensionTheme.components).forEach((componentName) => {
56-
if (duplicateComponents.includes(componentName)) {
57-
console.log('extension.getName()', extension.getName())
58-
const nameSpace = `${extension.getName()}/${componentName}`
59-
extensionTheme.components[nameSpace] = extensionTheme.components[componentName]
60-
delete extensionTheme.components[componentName]
61-
}
62-
})
6336
return extensionTheme
6437
})
6538
.filter((ex) => Object.keys(ex).length !== 0)
6639
const theme: ThemeOverride = extendTheme(...extensionThemes, baseExtensionTheme)
67-
68-
console.log('theme', theme)
40+
// console.log('theme', theme)
6941
return theme
7042
}

packages/extension-chakra-base/src/pages/sample.tsx

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/extension-chakra-base/src/setup-app.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ import {RouteProps} from 'react-router-dom'
1212
// Platform Imports
1313
import {ApplicationExtension} from '@salesforce/pwa-kit-extension-sdk/react'
1414

15-
// Local Imports
16-
import SamplePage from './pages/sample'
17-
18-
// Overridable Imports
19-
// Using the `overridable` loader means that you are opting in to the override module resolution flow. As a result this module
20-
// will be resolved by first looking in the base projects `overrides` folder then the overrides folders of any extensions configured
21-
// after this one. Only if no module is found will the referenced module in this project be used.
22-
// import sampleHOC from 'overridable!./components/sample-hoc'
2315
import {withChakraProvider} from './components/with-chakra-provider'
2416
// Others
2517
import extensionMeta from '../extension-meta.json'
@@ -47,14 +39,7 @@ class ChakraBaseExtension extends ApplicationExtension<Config> {
4739
* NOTE: If you instead want to modify a list of all the routes, refer to the `beforeRouteMatch` below.
4840
*/
4941
extendRoutes(routes: RouteProps[]): RouteProps[] {
50-
return [
51-
{
52-
exact: true,
53-
path: this.getConfig().path,
54-
component: SamplePage
55-
},
56-
...routes
57-
]
42+
return routes
5843
}
5944

6045
/**
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"id": "@salesforce/extension-chakra-store-locator"
3-
}
2+
"id": "@salesforce/extension-chakra-store-locator",
3+
"name": "ChakraStoreLocator"
4+
}

packages/extension-chakra-store-locator/src/components/heading.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
import React from 'react'
99
import {Heading, Badge, useMultiStyleConfig, SystemStyleObject} from '@chakra-ui/react'
1010
import metaData from '../../extension-meta.json'
11-
import {useApplicationExtension} from '@salesforce/pwa-kit-extension-sdk/react'
1211

12+
/**
13+
* Note: for any custom component for extension, the name space convention needs to be `${extensionName}/<componanyName>
14+
* The reason we need this convention is to assure extensions components theme
15+
* can work well with other extensions when installed in a single project. This will ensure the name will be unique per theme
16+
*/
1317
export const StoreLocatorHeading = (): JSX.Element => {
14-
const extension = useApplicationExtension(metaData.id)
15-
let styles: Record<string, SystemStyleObject>
16-
styles = useMultiStyleConfig(`${extension?.getName()}/StoreHeading`)
17-
if (Object.keys(styles).length === 0) {
18-
styles = useMultiStyleConfig('StoreHeading')
19-
}
18+
const styles = useMultiStyleConfig(`${metaData.name}/StoreHeading`)
2019
return (
2120
<Heading sx={styles.heading}>
2221
<>

packages/extension-chakra-store-locator/src/setup-app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class StoreLocatorExtension extends ApplicationExtension<Config> {
5959
]
6060
}
6161

62+
// TODO: find a better place for this method
6263
public getTheme(): ThemeOverride {
6364
return theme
6465
}

packages/extension-chakra-store-locator/src/theme/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import StoreHeading from '../theme/components/project/store-heading'
8+
import metaData from '../../extension-meta.json'
89

910
const colors = {
1011
transparent: 'transparent',
@@ -31,7 +32,7 @@ const colors = {
3132
}
3233
}
3334
const components = {
34-
StoreHeading
35+
[`${metaData.name}/StoreHeading`]: StoreHeading
3536
}
3637

3738
export default {
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"id": "@salesforce/extension-chakra-storefront"
3-
}
2+
"id": "@salesforce/extension-chakra-storefront",
3+
"name": "ChakraStorefront"
4+
}

0 commit comments

Comments
 (0)