ui.shipaid.com
v0.3.40
Published
This is the repository for the ShipAid Shopify (and possibly others in future) widget. It uses the [Lit](https://lit.dev/) web components library, so it is recommended to become familiar with it before contributing to this repository.
Downloads
6,228
Keywords
Readme
ShipAid Shopify Widget
This is the repository for the ShipAid Shopify (and possibly others in future) widget. It uses the Lit web components library, so it is recommended to become familiar with it before contributing to this repository.
Using Lit provides a framework that allows us to build a reactive UI, using JSX-like syntax - no need to use JQuery etc. And it can easily be installed in a page by using the custom web component name:
<shipaid-widget>Fallback Content</shipaid-widget>
Overview
This widget provides an interface where a user can choose to add or remove ShipAid protection - this is actually a product in their store that can be added to cart. When the component is loaded, we immediately run a request to fetch the ShipAid protection product details from our API, as well as the customers current cart from the Shopify AJAX API. Once we have this data, we can check whether the customer currently has the ShipAid product in their cart, and show either the add or remove product buttons based on this.
We also emit various custom events when we add or remove the ShipAid product from the cart, so other developers can listen to these events, and update AJAX carts.
Installation
Add the script tag to the theme - if the theme has an ajax cart, you'll likely want to add this to the theme.liquid
file, otherwise if the store has only a cart page (/cart
), then you can add it to just that page, to save it being unecessarily loaded when it isn't needed.
As we don't want to affect a stores speed at all, you should add it to the bottom of the page just before the ending body tag (</body>
), rather than inside the <head>
block.
<!-- ShipAid Widget -->
<script src="https://unpkg.com/ui.shipaid.com/dist/widget.es.js" type="module"></script>
Then add the widget element where needed:
<shipaid-widget></shipaid-widget>
<!-- Disable polling example -->
<shipaid-widget disablePolling></shipaid-widget>
<!-- With customised text -->
<shipaid-widget>
<p slot="loading">Loading ShipAid Protection</p>
</shipaid-widget>
Test Mode
Sometimes, a store won't have activated their subscription before you install the widget - in this case, the widget does not display (you will notice a message in the console reflecting this). So to force the widget to show while you are installing and testing it, you can add this query param to the page URL: shipaid-test
.
For example: https://some-store.myshopify.com/cart?shipaid-text
Slots
Slots, with the syntax slot="<slot name>"
, can be used to customise the widgets content - for example, a merchant may want to add a custom subtitle, which can be done like so:
<shipaid-widget>
<p slot="subtitle">Shipping protection is required to be able to return or refunds items.</p>
</shipaid-widget>
The default content will be replaced by any slot content. You can also add inline styles to the slots, if you need to change the font size/weight for example - but color changes should be handled by CSS variables:
<span slot="title" style="font-weight: 500;">Package Protection</span>
| Name | Description |
|--------|-------------|
| loading
| Replaces the text that is shown whilst the widget is loading - I.e. fetching content from the Shopify or ShipAid API's. |
| title
| Replaces the default title. |
| subtitle
| Replaces the default subtitle. |
Props
This is a list of props that can be used to configure the widget:
| Prop | Description | Value/Type |
|--------|-------------|---------|
| disablePolling
| Sets whether the cart should disable polling (enabled by default) - should be disabled if integrating manually with an ajax cart. | boolean
|
| pollingInterval
| If polling is enabled, it sets the interval (in ms) between API updates. | number
in milliseconds |
| disableRefresh
| Sets whether the store cart should be updated when the protection item is added/removed. Add if you want to initially hide the protection product from the cart, even if it has just been added. | boolean
|
| customerId
| Passes the information of the customer to the widget. Add if merchant wants to disable auto opt-in for some customers based on the customer tags. | boolean
|
| lang
| Sets the widget language (see the translations section below). This value can be any supported ISO 639-1 code. | string
defaults to en
|
| refreshCart
| Refresh the page if shipaid product quantity is greater than one to sync product qty at checkout | boolean
|
| persistPopup
| Use local storage to show popup so that popup don't re-render on script refresh | boolean
|
Events
This is a list of the events emitted by the widget. You can listen to these events like so:
document.addEventListener('shipaid-protection-status', ({ detail }) => {
console.log(detail.cart) // ShopifyCart
})
| Event | Description | Payload |
|-------|-------------|---------|
| shipaid-loaded
| Dispatched when the widget has finished fetching data from the API, and has successfully rendered. | Contains the data from the ShipAid API: ShipAidStore
|
| shipaid-protection-status
| Dispatched when a user either adds or removes the protection product from their cart. | { protection: boolean, cart: ShopifyCart, lineItem: ShopifyCartItem }
|
Methods
This is a list of public methods available on the widget HTMLElement that can be used to change protection settings. These can be used by querying the element:
const shipAidEl = document.querySelector('shipaid-widget')
if (shipAidEl) await shipAidEl.updateCart()
| Method | Description | Payload |
|--------|-------------|---------|
| hasProtection
| Returns a boolean which indicates whether the protection item is currently in the cart. | |
| updateCart
| Updates the internal cart, and triggers any protection product updates - use this method with with ajax carts, to update protection state without needing to reload the page. | Optional - the cart object from the ajax API. |
| addProtection
| Adds the relevant protection item to cart. | |
| removeProtection
| Removes the protection item from the cart. | |
Styles
If you need to change any of the widget colors to suit a specific theme, there are various CSS variables you can use to do so. These should be added to the style attribute on the widget element:
<shipaid-widget
style="
--shipaid-text: #fff;
--shipaid-prompt-actions-price-color: #fff;
"
>
</shipaid-widget>
| Variable | Description | Default |
|----------|-------------|---------|
| --shipaid-text
| Changes the global text color. | #000000
|
| --shipaid-text-muted
| Changes the global muted text color. | #aaaaaa
|
| --shipaid-prompt-margin
| Changes the margins of the main container element. | 2rem 0px 4rem auto
|
| --shipaid-prompt-width
| Changes the width of the main container element. | 400px
|
| --shipaid-prompt-width-mobile
| Changes the mobile width of the main container element. | 100%
|
| --shipaid-prompt-actions-price-color
| Changes the color of the price element. | var(--shipaid-text-muted)
|
| --shipaid-prompt-actions-button-color
| Changes the color of the add/remove button element. | var(--shipaid-primary)
|
| --shipaid-prompt-badge-background-color
| Changes the background color of the learn more button element. | var(--shipaid-light-grey)
|
| --shipaid-prompt-badge-text-color
| Changes the color of the learn more button element. | var(--shipaid-text)
|
| --shipaid-logo-height
| Changes the height of ShipAid logo. | var(--shipaid-logo-height, 35px)
|
| --shipaid-logo-max-height
| Changes the max height of ShipAid logo. | var(--shipaid-logo-max-height, 35px)
|
| --shipaid-logo-width
| Changes the width of ShipAid logo. | var(--shipaid-logo-width, auto)
|
| --shipaid-logo-max-width
| Changes the max width of ShipAid logo. | var(--shipaid-logo-max-width, 50px)
|
| --shipaid-prompt-footer-button-size
| Changes the height of info button in footer. | var(--shipaid-prompt-footer-button-size, 10px)
|
| --shipaid-prompt-badge-border-radius
| Changes border radius of footer | var(--shipaid-prompt-badge-border-radius, 30px)
|
| --shipaid-footer-badge-logo-height
| Changes the height of logo in footer. | var(--shipaid-footer-badge-logo-height, 9px)
|
| --shipaid-prompt-footer-location
| Changes the position of footer badge | var(--shipaid-prompt-footer-location, flex-start)
|
| --shipaid-prompt-product-actions-content
| Changes the spaces between price and add/remove button | var(--shipaid-prompt-product-actions-content, space-between)
|
| --shipaid-prompt-footer-topMargin
| Changes margin between header and badge footer | var(--shipaid-prompt-footer-topMargin, 0px)
|
| -shipaid-prompt-footer-display
| Changes the display option for footer | var(-shipaid-prompt-footer-display, flex)
|
Other variables can be found here (/widget/src/assets/styles.ts
).
Translations
This widget also supports multiple languages, using the lit-translate
plugin. The widget language can be configured using an attribute and would usally be static, but supports reactively swapping the language if a theme needs - i.e. if a user switches the language using a select option in the theme, then the widget language could be updated at the same time by selecting the widget element, and setting the lang
attribute.
To create lang files, you should copy the default /lang/en.json
file, and rename it to an ISO 639-1 code - for example cp /src/lang/en.json /src/lang/fs.json
. You can then go through each key, and translate the existing text to the relevant language. This should be all you need to do - the widget automatically (and lazily, to save bundle size) imports the relevant lang file when it is specified. It will also fallback to the default en.json
lang file if a certain language doesn't exist yet.
Contributing
Requirements
- Node v16+
- PNPM
- Development Shopify store (with the ShipAid app installed)
- ShipAid API development/staging instance
Development
You will need to make sure your development store has the ShipAid app installed, so the store and its protection product is added to the DB. You will also need to ensure the Shopify app you are testing this with has an app proxy added, and pointed towards an API instance.
pnpm install
pnpm run develop
Once the project is running, add the below to your development Shopify store theme.liquid
:
<!-- Dev -->
<script src="http://localhost:3000/src/shipaid-widget.ts"type="module" ></script>
<!-- Prod -->
<!-- ShipAid Widget -->
<script src="https://unpkg.com/ui.shipaid.com/dist/widget.es.js" type="module"></script>
And add the widget element where needed:
<shipaid-widget>
<p>Loading ShipAid Protection</p>
</shipaid-widget>
Build
Once the project has been built, you can publish the project to NPM, and users can add the script to their store using a package CDN (I.e. Unpkg).
pnpm install
pnpm run lint
pnpm run build
pnpm publish