@topsort/building-blocks
Topsort's JS library for building individual and easily integratable widgets.
Table of Contents
Integration
-
Insert a script in your site's <head> tag to load the Topsort Building Blocks library. Ensure the script has defer to load it without affecting your site load times. Replace x.y.z with the desired version, or use @latest to use the latest version of the library at all times.
<head>
<script
defer
src="https://unpkg.com/@topsort/building-blocks@x.y.z/dist/index.js"
></script>
</head>
-
After the DOM is loaded, create an instance of the TopsortBlocks library and initialize it. For example:
document.addEventListener("DOMContentLoaded", async () => {
const tsBlocks = new TopsortBlocks();
await tsBlocks.init({
apiKey: "api-key-123",
externalVendorId: "vendor-id-123",
});
});
Product Promotion Modal
-
Initialize product promotion:
With defaults:
tsBlocks.initProductPromotion();
With custom props:
tsBlocks.initProductPromotion(
promoteTargetClassName: "my-custom-promote-target",
style: {
primaryColorRgb: "50, 175, 200",
secondaryColorRgb: "50, 175, 200",
fontColorRgb: [50, 175, 200],
button: {
borderRadius: "none" | "sm" | "full"
},
},
text: {
promoteButton: "Create Campaign",
detailButton: "View Campaign",
},
);
-
In your markup, add the following HTML class and data attributes to the element(s) you want a Promote button appended to:
<div
class="ts-promote-target some-other-custom-class"
data-ts-product-id="product-id-123"
data-ts-product-name="My Product"
data-ts-product-img-url="https://picsum.photos/100"
>
...
</div>
This target class is also a static property of the TopsortBlocks class:
<div
class={`${TopsortBlocks.promoteTargetClassName} my-custom-class`}
data-ts-product-id="product-id-123"
data-ts-product-name="My Product"
data-ts-product-img-url="https://picsum.photos/100"
>
...
</div>
If you are using a custom promote target class name, use it instead:
<div
class="my-custom-promote-target my-custom-class"
data-ts-product-id="product-id-123"
data-ts-product-name="My Product"
data-ts-product-img-url="https://picsum.photos/100"
>
...
</div>
Development
Running the demo
-
Install the dependencies:
npm install
-
Create and set up your .env file:
cp .env.example .env
-
Start the development server which runs the demo at localhost:8080:
npm run start
Adding new environment variables
Environment variables should not contain secrets (e.g. private API keys) since they will be injected into the published build. They are meant to change the behaviour between different environments such as calling a local, staging, or production Central Services API base URL.
To add a new env var:
-
Add its name to .env.example with an empty or default value:
MY_NEW_PUBLIC_VAR=
-
Add its name and value to your .env:
MY_NEW_PUBLIC_VAR="the actual value"
-
To have it injected into the app, add it to webpack.common.js in the DefinePlugin:
Remember to wrap the value in JSON.stringify() because the DefinePlugin does a find-and-replace and the value given to it must include actual quotes inside of the string itself.
For reference, see the Tip on the Webpack DefinePlugin docs.
new webpack.DefinePlugin({
MY_NEW_PUBLIC_VAR: JSON.stringify(process.env.MY_NEW_PUBLIC_VAR),
}),
-
To inform TypeScript of this new global variable, add it to src/types.ts under the global declaration with its type:
declare global {
const MY_NEW_PUBLIC_VAR: string;
}
-
To inform eslint of this new global variable, add it to .eslintrc.js under globals with a readonly value (assuming it's meant to be readonly):
globals: {
MY_NEW_PUBLIC_VAR: "readonly",
},
-
Ensure you restart your dev server so webpack can pick up the latest changes.