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

Bring back swatch-input snippet #3299

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions assets/component-product-variant-picker.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ variant-selects {
width: 1px;
}

.product-form__input input[type='radio']:not(.disabled) + label > .label-unavailable {
.product-form__input input[type='radio']:not(.disabled):not(.visually-disabled) + label > .label-unavailable {
display: none;
}

Expand All @@ -43,7 +43,6 @@ variant-selects {
margin-bottom: 1.6rem;
}


.product-form__input--dropdown .dropdown-swatch + select {
padding-left: calc(2.4rem + var(--swatch-input--size));
}
Expand All @@ -57,7 +56,6 @@ variant-selects {
z-index: 1;
}


/* Custom styles for Pill display type */
.product-form__input--pill input[type='radio'] + label {
border: var(--variant-pills-border-width) solid rgba(var(--color-foreground), var(--variant-pills-border-opacity));
Expand Down
8 changes: 4 additions & 4 deletions assets/component-swatch-input.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}

/* Visually disabled */
.swatch-input__input.disabled:not(:active):not(:checked) + .swatch-input__label:hover {
.swatch-input__input.visually-disabled:not(:active):not(:checked) + .swatch-input__label:hover {
outline: none;
}

Expand All @@ -35,14 +35,14 @@

/* Overrides for swatch snippet when used inside disabled swatch-input */
.swatch-input__input:disabled + .swatch-input__label > .swatch,
.swatch-input__input.disabled + .swatch-input__label > .swatch {
.swatch-input__input.visually-disabled + .swatch-input__label > .swatch {
position: relative;
overflow: hidden;
}

/* Display white semi-transparent overlay over swatch when input is disabled */
.swatch-input__input:disabled + .swatch-input__label > .swatch::before,
.swatch-input__input.disabled + .swatch-input__label > .swatch::before {
.swatch-input__input.visually-disabled + .swatch-input__label > .swatch::before {
content: '';
position: absolute;
inset: 0;
Expand All @@ -51,7 +51,7 @@

/* Display crossed out line over swatch when input is disabled */
.swatch-input__input:disabled + .swatch-input__label > .swatch::after,
.swatch-input__input.disabled + .swatch-input__label > .swatch::after {
.swatch-input__input.visually-disabled + .swatch-input__label > .swatch::after {
/* Diagonal of a square = length of the side * sqrt(2) */
--diagonal--size: calc(var(--swatch-input--size) * 1.414);
--crossed-line--size: 0.1rem;
Expand Down
43 changes: 18 additions & 25 deletions snippets/product-variant-options.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -52,50 +52,43 @@
elsif value.swatch.color
assign swatch_value = 'rgb(' | append: value.swatch.color.rgb | append: ')'
else
assign swatch_value = nil
assign swatch_value = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? Liquid is ruby, I would expect nil to be the correct empty state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and is what is configured for the Shopify Liquid plugin. It automatically changes those for me. I asked @charlespwd about this and he linked me to this bit of code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a video of the formatter automatically changing nil to null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jas7457 you have unlocked secret Shopify knowledge. Mind cutting an update the the Dev docs that records null as a falsy value? 🙏🏻 https://shopify.dev/docs/api/liquid/basics#truthy-and-falsy

Jason, probably

endif
-%}

{%- capture input_id -%}
{{ section.id }}-{{ option.position }}-{{ forloop.index0 -}}
{%- endcapture -%}

{%- capture label_unavailable %}
{%- capture label_unavailable -%}
<span class="visually-hidden label-unavailable">
{{- 'products.product.variant_sold_out_or_unavailable' | t -}}
</span>
{%- endcapture %}
{%- endcapture -%}

{%- if picker_type == 'swatch' -%}
{% liquid
assign checked = false

if option.selected_value == value
assign checked = true
endif

assign value_text = value | escape
%}
<input
type="radio"
id="{{ input_id }}"
name="{{ option.name }}"
value="{{ value_text }}"
form="{{ product_form_id }}"
class="swatch-input__input{% if option_disabled %} disabled{% endif %}"
{% if checked %}
checked
{% endif %}
>
<label
for="{{ input_id }}"
title="{{ value_text }}"
class="swatch-input__label{% if block.settings.swatch_shape == 'square' %} swatch-input__label--square{% endif %}"
>
{% render 'swatch', swatch: value.swatch, shape: block.settings.swatch_shape %}
<span class="visually-hidden">{{ value_text }}</span>
{%- capture help_text -%}
<span class="visually-hidden">{{ value | escape }}</span>
{{ label_unavailable }}
</label>
{%- endcapture -%}
{%
render 'swatch-input',
id: input_id,
name: option.name,
value: value | escape,
swatch: value.swatch,
product_form_id: product_form_id,
checked: checked,
visually_disabled: option_disabled,
shape: block.settings.swatch_shape,
help_text: help_text
%}
{%- elsif picker_type == 'button' -%}
<input
type="radio"
Expand Down
48 changes: 48 additions & 0 deletions snippets/swatch-input.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% comment %}
Renders a swatch input component.
Accepts:
- id: {String} unique input id
- type: {String} input type. Accepts 'radio' or 'checkbox', defaults to 'radio' (optional)
- name: {String} input name,
- value: {ProductOptionValueDrop} input value
- swatch: {SwatchDrop} the swatch drop
- product_form_id: {String} id of the form associted with the input
- checked: {Boolean} default checked status
- disabled: {Boolean} default disabled status (optional)
- visually_disabled: {Boolean} style the swatch as disabled, but leave the input enabled (optional)
- shape: {String} swatch shape. Accepts 'square', defaults to circle (optional)
- help_text: {String} additional content to render inside the label (optional)

Usage:
{% render 'swatch-input',
id: input_id,
name: input_name,
value: input_value,
swatch: swatch,
product_form_id: product_form_id,
checked: checked
%}
{% endcomment %}

<input
type="{{ type | default: 'radio' }}"
id="{{ id }}"
name="{{ name }}"
value="{{ value }}"
form="{{ product_form_id }}"
class="swatch-input__input{% if visually_disabled %} visually-disabled{% endif %}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because .disabled has been updated to .visually_disabled, the CSS below is no longer applied correctly to label-unavailable and the "Variant sold out or unavailable" message isn't read by screen readers for unavailable variants.

.product-form__input input[type='radio']:not(.disabled) + label > .label-unavailable {
display: none;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - updated

{% if checked %}
checked
{% endif %}
{% if disabled %}
disabled
{% endif %}
>
<label
for="{{ id }}"
title="{{ value }}"
class="swatch-input__label{% if shape == 'square' %} swatch-input__label--square{% endif %}"
>
{% render 'swatch', swatch: swatch, shape: shape %}
{{ help_text }}
</label>
Loading