0.3.26 • Published 6 days ago

ui.shipaid.com v0.3.26

Weekly downloads
-
License
-
Repository
-
Last release
6 days ago

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>
NameDescription
loadingReplaces the text that is shown whilst the widget is loading - I.e. fetching content from the Shopify or ShipAid API's.
titleReplaces the default title.
subtitleReplaces the default subtitle.

Props

This is a list of props that can be used to configure the widget:

PropDescriptionValue/Type
disablePollingSets whether the cart should disable polling (enabled by default) - should be disabled if integrating manually with an ajax cart.boolean
pollingIntervalIf polling is enabled, it sets the interval (in ms) between API updates.number in milliseconds
disableRefreshSets 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
customerIdPasses 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
langSets the widget language (see the translations section below). This value can be any supported ISO 639-1 code.string defaults to en
refreshCartRefresh the page if shipaid product quantity is greater than one to sync product qty at checkoutboolean
persistPopupUse local storage to show popup so that popup don't re-render on script refreshboolean

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
})
EventDescriptionPayload
shipaid-loadedDispatched when the widget has finished fetching data from the API, and has successfully rendered.Contains the data from the ShipAid API: ShipAidStore
shipaid-protection-statusDispatched 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()
MethodDescriptionPayload
hasProtectionReturns a boolean which indicates whether the protection item is currently in the cart.
updateCartUpdates 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.
addProtectionAdds the relevant protection item to cart.
removeProtectionRemoves 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>
VariableDescriptionDefault
--shipaid-textChanges the global text color.#000000
--shipaid-text-mutedChanges the global muted text color.#aaaaaa
--shipaid-prompt-marginChanges the margins of the main container element.2rem 0px 4rem auto
--shipaid-prompt-widthChanges the width of the main container element.400px
--shipaid-prompt-width-mobileChanges the mobile width of the main container element.100%
--shipaid-prompt-actions-price-colorChanges the color of the price element.var(--shipaid-text-muted)
--shipaid-prompt-actions-button-colorChanges the color of the add/remove button element.var(--shipaid-primary)
--shipaid-prompt-badge-background-colorChanges the background color of the learn more button element.var(--shipaid-light-grey)
--shipaid-prompt-badge-text-colorChanges the color of the learn more button element.var(--shipaid-text)
--shipaid-logo-heightChanges the height of ShipAid logo.var(--shipaid-logo-height, 35px)
--shipaid-logo-max-heightChanges the max height of ShipAid logo.var(--shipaid-logo-max-height, 35px)
--shipaid-logo-widthChanges the width of ShipAid logo.var(--shipaid-logo-width, auto)
--shipaid-logo-max-widthChanges the max width of ShipAid logo.var(--shipaid-logo-max-width, 50px)
--shipaid-prompt-footer-button-sizeChanges the height of info button in footer.var(--shipaid-prompt-footer-button-size, 10px)
--shipaid-prompt-badge-border-radiusChanges border radius of footervar(--shipaid-prompt-badge-border-radius, 30px)
--shipaid-footer-badge-logo-heightChanges the height of logo in footer.var(--shipaid-footer-badge-logo-height, 9px)
--shipaid-prompt-footer-locationChanges the position of footer badgevar(--shipaid-prompt-footer-location, flex-start)
--shipaid-prompt-product-actions-contentChanges the spaces between price and add/remove buttonvar(--shipaid-prompt-product-actions-content, space-between)
--shipaid-prompt-footer-topMarginChanges margin between header and badge footervar(--shipaid-prompt-footer-topMargin, 0px)
-shipaid-prompt-footer-displayChanges the display option for footervar(-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
0.3.26

6 days ago

0.3.25

6 days ago

0.3.24

11 days ago

0.3.23

11 days ago

0.3.22

19 days ago

0.3.21

26 days ago

0.3.20

1 month ago

0.3.19

2 months ago

0.3.18

2 months ago

0.3.17

2 months ago

0.3.16

4 months ago

0.3.15

5 months ago

0.3.14

5 months ago

0.3.13

5 months ago

0.3.12

7 months ago

0.3.6

9 months ago

0.3.5

10 months ago

0.3.8

9 months ago

0.3.7

9 months ago

0.3.2

11 months ago

0.3.4

10 months ago

0.3.3

11 months ago

0.3.9

9 months ago

0.3.11

8 months ago

0.3.10

8 months ago

0.3.0-beta.2

1 year ago

0.3.0

1 year ago

0.3.0-beta.1

1 year ago

0.3.1

1 year ago

0.2.23

1 year ago

0.2.22

1 year ago

0.2.21

1 year ago

0.2.20

1 year ago

0.2.19

1 year ago

0.2.18

1 year ago

0.2.13-beta

2 years ago

0.2.17

1 year ago

0.2.16

1 year ago

0.2.15

1 year ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.18-beta.1

1 year ago

0.2.13-beta.4

2 years ago

0.2.13-beta.3

2 years ago

0.2.13-beta.2

2 years ago

0.2.17-beta.1

1 year ago

0.2.14-beta.1

2 years ago

0.2.13-beta.5

2 years ago

0.2.10

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.5

2 years ago

0.1.0

2 years ago

0.2.1

2 years ago

0.1.2

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.8

2 years ago

0.0.9

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.2.3

2 years ago

0.1.4

2 years ago

0.2.2

2 years ago

0.1.3

2 years ago

0.1.6

2 years ago

0.2.4

2 years ago

0.1.5

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.0.0

2 years ago