Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Javascript Types w/JSDoc #3577

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9278173
docs(jsdoc): :memo: add documentation to `global.js`
joshistoast Aug 2, 2024
ffac3c0
style(jsdoc): :fire: remove unnecessary `@function` specification
joshistoast Aug 2, 2024
4574c06
docs: :memo: document `animations.js`
joshistoast Aug 6, 2024
d0e7b15
docs: :memo: document `cart-drawer.js`
joshistoast Aug 6, 2024
b3c3ae3
Merge branch 'main' into docs/document-javascripts
joshistoast Aug 6, 2024
8ae79f4
docs: :memo: document more cart functions
joshistoast Aug 7, 2024
3c82907
docs: :memo: document `customer.js`
joshistoast Aug 8, 2024
8e11104
docs: :memo: document `details-disclosure.js`
joshistoast Aug 8, 2024
54a0b1e
docs: :memo: document `details-modal.js`
joshistoast Aug 8, 2024
81331d9
docs: :memo: document `facets.js`
joshistoast Aug 9, 2024
48d5563
docs: :memo: document `localization-form.js`
joshistoast Aug 9, 2024
0f27385
docs: :memo: document `magnify.js`
joshistoast Aug 12, 2024
e5b8254
docs: :memo: document `search-form.js`
joshistoast Aug 12, 2024
01ca67c
docs: :memo: document `main-search.js`
joshistoast Aug 12, 2024
a4b0cd0
docs: :memo: document `media-gallery.js`
joshistoast Aug 12, 2024
a05fc38
docs: :memo: document `password-modal.js`
joshistoast Aug 12, 2024
21b0080
docs: :memo: document `pickup-availability.js`
joshistoast Aug 12, 2024
179f8cd
docs: :memo: document `predictive-search.js`
joshistoast Aug 12, 2024
40d00ba
style: :memo: inline some search form doc comments
joshistoast Aug 12, 2024
49e098c
docs: :memo: document `price-per-item.js`
joshistoast Aug 12, 2024
8f8af35
docs: :memo: document `fetchConfig` and `ModalDialog` class
joshistoast Aug 13, 2024
a791ff4
docs: :memo: document global Shopify functions
joshistoast Aug 13, 2024
95b73d8
docs: :memo: document `pubsub.js`
joshistoast Aug 13, 2024
34c6d11
docs: :memo: document constants and pub/sub events
joshistoast Aug 13, 2024
5bda0b6
docs: :memo: update `customers.js` documentation
joshistoast Aug 13, 2024
afc2b49
docs: :memo: document `product-form.js`
joshistoast Aug 13, 2024
2d08539
docs: :memo: document `quick-add.js`
joshistoast Aug 13, 2024
aced753
docs: :memo: document `product-info.js`
joshistoast Aug 13, 2024
f0ac0e7
fix: :pencil2: correct typo on `setQuantityBoundries`
joshistoast Aug 19, 2024
a756192
docs: :memo: add modal types to quick add
joshistoast Aug 19, 2024
ea40ea2
docs: :memo: add types to slideshow + document theme editor js
joshistoast Aug 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions assets/animations.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// Class names for scroll animations
const SCROLL_ANIMATION_TRIGGER_CLASSNAME = 'scroll-trigger';
const SCROLL_ANIMATION_OFFSCREEN_CLASSNAME = 'scroll-trigger--offscreen';
const SCROLL_ZOOM_IN_TRIGGER_CLASSNAME = 'animate--zoom-in';
const SCROLL_ANIMATION_CANCEL_CLASSNAME = 'scroll-trigger--cancel';

// Scroll in animation logic
/**
* Callback for the IntersectionObserver.
* Handles adding/removing animation classes based on the element's visibility in the viewport.
* @param {IntersectionObserverEntry[]} elements - The elements being observed.
* @param {IntersectionObserver} observer - The IntersectionObserver instance.
*/
function onIntersection(elements, observer) {
elements.forEach((element, index) => {
if (element.isIntersecting) {
Expand All @@ -21,6 +27,11 @@ function onIntersection(elements, observer) {
});
}

/**
* Initialize scroll animation triggers.
* @param {HTMLElement} [rootEl=document] - The root element to search for scroll animation triggers.
* @param {boolean} [isDesignModeEvent=false] - Flag to indicate if the function is being called from a Shopify design mode event.
*/
function initializeScrollAnimationTrigger(rootEl = document, isDesignModeEvent = false) {
const animationTriggerElements = Array.from(rootEl.getElementsByClassName(SCROLL_ANIMATION_TRIGGER_CLASSNAME));
if (animationTriggerElements.length === 0) return;
Expand All @@ -38,7 +49,9 @@ function initializeScrollAnimationTrigger(rootEl = document, isDesignModeEvent =
animationTriggerElements.forEach((element) => observer.observe(element));
}

// Zoom in animation logic
/**
* Initialize scroll zoom animation triggers.
*/
function initializeScrollZoomAnimationTrigger() {
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;

Expand Down Expand Up @@ -71,6 +84,11 @@ function initializeScrollZoomAnimationTrigger() {
});
}

/**
* Calculates percentage of element visible in viewport.
* @param {HTMLElement} element - The element to calculate the visibility percentage of.
* @returns {number} The visibility percentage of the element.
*/
function percentageSeen(element) {
const viewportHeight = window.innerHeight;
const scrollY = window.scrollY;
Expand All @@ -91,11 +109,13 @@ function percentageSeen(element) {
return Math.round(percentage);
}

// Initialize animations on DOM content loaded
window.addEventListener('DOMContentLoaded', () => {
initializeScrollAnimationTrigger();
initializeScrollZoomAnimationTrigger();
});

// Shopify design mode event listeners
if (Shopify.designMode) {
document.addEventListener('shopify:section:load', (event) => initializeScrollAnimationTrigger(event.target, true));
document.addEventListener('shopify:section:reorder', () => initializeScrollAnimationTrigger(document, true));
Expand Down
49 changes: 49 additions & 0 deletions assets/cart-drawer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* Cart Drawer custom element class.
* @extends HTMLElement
*/
class CartDrawer extends HTMLElement {
constructor() {
super();
Expand All @@ -7,6 +11,9 @@ class CartDrawer extends HTMLElement {
this.setHeaderCartIconAccessibility();
}

/**
* Sets accessibility attributes for header cart icon.
*/
setHeaderCartIconAccessibility() {
const cartLink = document.querySelector('#cart-icon-bubble');
if (!cartLink) return;
Expand All @@ -25,6 +32,10 @@ class CartDrawer extends HTMLElement {
});
}

/**
* Opens the cart drawer.
* @param {HTMLElement} [triggeredBy] - The element that triggered the open action.
*/
open(triggeredBy) {
if (triggeredBy) this.setActiveElement(triggeredBy);
const cartDrawerNote = this.querySelector('[id^="Details-"] summary');
Expand All @@ -49,12 +60,18 @@ class CartDrawer extends HTMLElement {
document.body.classList.add('overflow-hidden');
}

/**
* Closes the cart drawer.
*/
close() {
this.classList.remove('active');
removeTrapFocus(this.activeElement);
document.body.classList.remove('overflow-hidden');
}

/**
* Sets accessibility attributes for the cart drawer note.
*/
setSummaryAccessibility(cartDrawerNote) {
cartDrawerNote.setAttribute('role', 'button');
cartDrawerNote.setAttribute('aria-expanded', 'false');
Expand All @@ -70,6 +87,10 @@ class CartDrawer extends HTMLElement {
cartDrawerNote.parentElement.addEventListener('keyup', onKeyUpEscape);
}

/**
* Renders the cart drawer contents.
* @param {Object} parsedState - The parsed state of the cart.
*/
renderContents(parsedState) {
this.querySelector('.drawer__inner').classList.contains('is-empty') &&
this.querySelector('.drawer__inner').classList.remove('is-empty');
Expand All @@ -89,10 +110,20 @@ class CartDrawer extends HTMLElement {
});
}

/**
* Gets the inner HTML of a cart section.
* @param {string} html - The HTML string to parse.
* @param {string} [selector='.shopify-section'] - The selector to query for.
* @returns {string} The inner HTML of the matched section.
*/
getSectionInnerHTML(html, selector = '.shopify-section') {
return new DOMParser().parseFromString(html, 'text/html').querySelector(selector).innerHTML;
}

/**
* Array of section selectors for sections api to render.
* @returns {Array<Object>} The array of sections to render.
*/
getSectionsToRender() {
return [
{
Expand All @@ -105,18 +136,36 @@ class CartDrawer extends HTMLElement {
];
}

/**
* Gets the DOM of a cart drawer section.
* @param {string} html - The HTML string to parse.
* @param {string} [selector='.shopify-section'] - The selector to query for.
* @returns {HTMLElement} The DOM of the matched section.
*/
getSectionDOM(html, selector = '.shopify-section') {
return new DOMParser().parseFromString(html, 'text/html').querySelector(selector);
}

/**
* Sets the active element.
* @param {HTMLElement} element - The element to set as active.
*/
setActiveElement(element) {
this.activeElement = element;
}
}

customElements.define('cart-drawer', CartDrawer);

/**
* Cart Drawer Items custom element class.
* @extends CartItems
*/
class CartDrawerItems extends CartItems {
/**
* Array of section selectors for sections api to render.
* @returns {Array<Object>} The array of sections to render.
*/
getSectionsToRender() {
return [
{
Expand Down
34 changes: 34 additions & 0 deletions assets/cart-notification.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* Cart Notification custom element class.
* @extends HTMLElement
*/
class CartNotification extends HTMLElement {
constructor() {
super();

/** @type {HTMLElement} */
this.notification = document.getElementById('cart-notification');
/** @type {HTMLElement} */
this.header = document.querySelector('sticky-header');
this.onBodyClick = this.handleBodyClick.bind(this);

Expand All @@ -12,6 +18,9 @@ class CartNotification extends HTMLElement {
);
}

/**
* Open the cart notification.
*/
open() {
this.notification.classList.add('animate', 'active');

Expand All @@ -27,13 +36,20 @@ class CartNotification extends HTMLElement {
document.body.addEventListener('click', this.onBodyClick);
}

/**
* Close the cart notification.
*/
close() {
this.notification.classList.remove('active');
document.body.removeEventListener('click', this.onBodyClick);

removeTrapFocus(this.activeElement);
}

/**
* Replace DOM with parsed state from Sections API.
* @param {Object} parsedState - The parsed state object from Sections API.
*/
renderContents(parsedState) {
this.cartItemKey = parsedState.key;
this.getSectionsToRender().forEach((section) => {
Expand All @@ -47,6 +63,10 @@ class CartNotification extends HTMLElement {
this.open();
}

/**
* Array of sections to fetch from the Sections API.
* @returns {Array<Object>} Array of sections to fetch from the Sections API.
*/
getSectionsToRender() {
return [
{
Expand All @@ -62,10 +82,20 @@ class CartNotification extends HTMLElement {
];
}

/**
* Gets the inner HTML of a cart section.
* @param {string} html - The HTML string to parse.
* @param {string} [selector='.shopify-section'] - The selector to query for.
* @returns {string} The inner HTML of the matched section.
*/
getSectionInnerHTML(html, selector = '.shopify-section') {
return new DOMParser().parseFromString(html, 'text/html').querySelector(selector).innerHTML;
}

/**
* Handle body click events to close the cart notification if clicked outside.
* @param {MouseEvent} evt - The click event.
*/
handleBodyClick(evt) {
const target = evt.target;
if (target !== this.notification && !target.closest('cart-notification')) {
Expand All @@ -75,6 +105,10 @@ class CartNotification extends HTMLElement {
}
}

/**
* Sets the active element.
* @param {HTMLElement} element - The element to set as active.
*/
setActiveElement(element) {
this.activeElement = element;
}
Expand Down
Loading
Loading