New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@shopify/theme-addresses

Package Overview
Dependencies
Maintainers
13
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shopify/theme-addresses

A library that helps developers work with addresses within Shopify Themes

2.0.3
Source
npm
Version published
Weekly downloads
6.4K
-24.19%
Maintainers
13
Weekly downloads
 
Created
Source

@shopify/theme-addresses

theme-addresses.js helps developers work with common address releated operations, such as, building the country and province selectors

Getting Started

Theme Scripts can be used in any theme project. To take advantage of semantic versioning and easy updates, we recommend using NPM or Yarn to include them in your project:

yarn add @shopify/theme-addresses

and then import the functions you wish to use through ES6 imports:

import {CountryProvinceSelector} from '@shopify/theme-addresses';

If you prefer not to use a package manager, you can download the latest version of Theme Addresses and include it in your project manually from the following links:

  • theme-addresses.js
  • theme-addresses.min.js

Methods

CountryProvinceSelector(countryOptions)

Prepares an instance of a CountryProvinceSelector with the given country options

Example

// In theme.liquid, somewhere before the end of </head> tag
    ...
    {% if template.directory == 'customers' %}
      theme.allCountryOptionTags = {{ all_country_option_tags | json }};
    {% endif %}
  </script>
  ...
</head>
// In addresses.js
import {CountryProvinceSelector} from '@shopify/theme-addresses';

const billingCountryProvinceSelector = new CountryProvinceSelector(window.theme.allCountryOptionTags);

build(countryNodeElement, provinceNodeElement, options)

Populates the given <select> elements with country and province options.

  • countryNodeElement: The <select> node element for countries
  • provinceNodeElement: The <select> node element for provinces according to the selected country
  • options:
    • onCountryChange: The callback after a country change event
    • onProvinceChange: The callback after a province change event

The Select node element should look like the following:

    <select ... id="newCountry" name="address[country]" data-default="{{ form.country }}"></select>
    <select ... id="newProvince" name="address[province]" data-default="{{ form.province }}"></select>

Example

import {CountryProvinceSelector} from '@shopify/theme-addresses';

const billingCountryProvinceSelector = new CountryProvinceSelector(window.theme.allCountryOptionTags);

billingCountryProvinceSelector.build(document.querySelector('#newCountry'), document.querySelector('#newProvince'));
billingCountryProvinceSelector.build(document.querySelector('#editCountry'), document.querySelector('#editProvince'));

We do not dictate what selector library you can use in your themes as long as the element being passed in is a native Node element.

Example using jQuery

import $ from 'jquery';
import {CountryProvinceSelector} from '@shopify/theme-addresses';

const billingCountryProvinceSelector = new CountryProvinceSelector(window.theme.allCountryOptionTags);

billingCountryProvinceSelector.build($('#newCountry')[0], $('#newProvince')[0]);
billingCountryProvinceSelector.build($('#editCountry')[0], $('#editProvince')[0]);
options
onCountryChange(provinces, provinceNodeElement, countryNodeElement)

The callback when a change event happens on the country select.

  • provinces: The array of provinces of selected country
  • provinceNodeElement: The <select> node element for provinces
  • countryNodeElement: The <select> node element for countries
onProvinceChange(event)

The callback when a change event happens on the province select.

  • event: The change event object of the province select

Example

    <select ... id="newCountry" name="address[country]" data-default="{{ form.country }}"></select>
    <div id="newProvinceContainer">
        <p>Provinces</p>
        <select ... id="newProvince" name="address[province]" data-default="{{ form.province }}"></select>
    </div>
import {CountryProvinceSelector} from '@shopify/theme-addresses';

const newProvinceContainer = document.querySelector('#newProvinceContainer')
const billingCountryProvinceSelector = new CountryProvinceSelector(window.theme.allCountryOptionTags);

billingCountryProvinceSelector.build(
    document.querySelector('#newCountry'),
    document.querySelector('#newProvince'),
    {
        onCountryChange: (provinces) => {
            if (provinces.length) {
                newProvinceContainer.classList.remove('hide');
            } else {
                newProvinceContainer.classList.add('hide');
            }
        }
    });

Keywords

slate

FAQs

Package last updated on 21 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts