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

Size Selector #79

Closed
wants to merge 13 commits into from
160 changes: 87 additions & 73 deletions src/components/SizeSelector/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component } from 'pet-dex-utilities';
import sizeSmall from './img/size-small.svg';
import sizeMedium from './img/size-medium-active.svg';
import sizeLarge from './img/size-large.svg';
import sizeSmall from './images/size-small.svg';
import sizeMedium from './images/size-medium.svg';
import sizeLarge from './images/size-large.svg';
import './index.scss';

const events = ['click', 'touchstart'];
Expand All @@ -10,93 +10,107 @@ const html = `
<div class="selector-wrapper" data-select="selector-wrapper">
<ul role="listbox" class="selector-wrapper__slide" data-select="selector-wrapper__slide">
<li role="option" aria-label="small pet size" aria-selected="false" class="selector-wrapper__item" data-select="slider-small">
<img class="selector-wrapper__image" data-select="selector-wrapper__image" src="${sizeSmall}" alt="pet small size icon">
<h3>Small</h3>
<p>under 14kg</p>
<img class="selector-wrapper__image" src="${sizeSmall}" alt="pet small size icon">
<h3 class="selector-wrapper__title">Small</h3>
<p class="selector-wrapper__weight">under 14kg</p>
</li>
<li role="option" aria-label="medium pet size" aria-selected="true" class="selector-wrapper__item active" data-select="slider-medium">
<li role="option" aria-label="medium pet size" aria-selected="true" class="selector-wrapper__item" data-select="slider-medium">
<img class="selector-wrapper__image" src="${sizeMedium}" alt="pet medium size icon">
<h3>Medium</h3>
<p>14-25kg</p>
<h3 class="selector-wrapper__title">Medium</h3>
<p class="selector-wrapper__weight">14-25kg</p>
</li>
<li role="option" aria-label="large pet size" aria-selected="false" class="selector-wrapper__item" data-select="slider-large">
<img class="selector-wrapper__image" da src="${sizeLarge}" alt="pet large size icon">
<h3>Large</h3>
<p>over 25kg</p>
<h3 class="selector-wrapper__title">Large</h3>
<p class="selector-wrapper__weight">over 25kg</p>
</li>
</ul>
</div>
`;

export default function SizeSelector(slide) {
function classRemover(children) {
[...children].forEach((element) => {
element.classList.remove('selector-wrapper__item--active');
});
}

function changeToDefault(children) {
[...children].forEach((element) => {
const srcValue = element.children[0].attributes.src.value;
const itemSrc = element.children[0].attributes.src;

const srcMappings = {
'/components/SizeSelector/images/size-small-active.svg':
'/components/SizeSelector/images/size-small.svg',
'/components/SizeSelector/images/size-medium-active.svg':
'/components/SizeSelector/images/size-medium.svg',
'/components/SizeSelector/images/size-large-active.svg':
'/components/SizeSelector/images/size-large.svg',
};

const newValue = srcMappings[srcValue];

if (newValue) {
element.classList.remove('selector-wrapper__item--active');
element.setAttribute('aria-selected', 'false');

itemSrc.value = newValue;
}
});
}

function changeToActive(element) {
const srcValue = element.children[0].attributes.src.value;
const itemSrc = element.children[0].attributes.src;

const srcMappings = {
'/components/SizeSelector/images/size-small.svg':
'/components/SizeSelector/images/size-small-active.svg',
'/components/SizeSelector/images/size-medium.svg':
'/components/SizeSelector/images/size-medium-active.svg',
'/components/SizeSelector/images/size-large.svg':
'/components/SizeSelector/images/size-large-active.svg',
};

const newValue = srcMappings[srcValue];

if (newValue) {
element.classList.add('selector-wrapper__item--active');
element.setAttribute('aria-selected', 'true');

itemSrc.value = newValue;
}
}

function checkingTheElement(children) {
children.forEach((element) => {
element.addEventListener('click', () => {
const elementState = element.classList.contains(
'selector-wrapper__item--active',
);
changeToDefault(children);
if (elementState) {
changeToDefault(children);
} else {
changeToActive(element);
}
});
});
}

export default function SizeSelector({ slide }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export default function SizeSelector({ slide }) {
export default function SizeSelector({ slide } = {}) {

Component.call(this, { html, events });

const slideItems = this.selected.get(slide);
Copy link
Contributor

Choose a reason for hiding this comment

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

Essa seleção pelo data-select está muito estranha pra mim, o data-select não foi pensando pra isso
@Alecell

const children = [...slideItems.children];

const childrens = [...slideItems.children];
this.startEvents(childrens);
classRemover(children);
changeToDefault(children);
checkingTheElement(children);
}

SizeSelector.prototype = Object.assign(
SizeSelector.prototype,
Component.prototype,
{
startEvents(childrens) {
childrens.map((element) => {
element.addEventListener('pointerdown', () => {
this.changeToDefault(childrens);
if (element.classList.contains('active')) {
element.classList.add('active');
} else {
childrens.map((children) => {
children.classList.remove('active');
return children;
});
element.classList.add('active');
}
this.changeToActive(element);
});
return this;
});
},

changeToDefault(childrens) {
childrens.map((element) => {
const srcValue = element.children[0].attributes.src.value;
const itemSrc = element.children[0].attributes.src;

if (srcValue === '/components/SizeSelector/img/size-small-active.svg') {
itemSrc.value = '/components/SizeSelector/img/size-small.svg';
element.setAttribute('aria-selected', 'false');
} else if (
srcValue === '/components/SizeSelector/img/size-medium-active.svg'
) {
itemSrc.value = '/components/SizeSelector/img/size-medium.svg';
element.setAttribute('aria-selected', 'false');
} else if (
srcValue === '/components/SizeSelector/img/size-large-active.svg'
) {
itemSrc.value = '/components/SizeSelector/img/size-large.svg';
element.setAttribute('aria-selected', 'false');
}
return srcValue;
});
},

changeToActive(element) {
const srcValue = element.children[0].attributes.src.value;
const itemSrc = element.children[0].attributes.src;

if (srcValue === '/components/SizeSelector/img/size-small.svg') {
itemSrc.value = '/components/SizeSelector/img/size-small-active.svg';
element.setAttribute('aria-selected', 'true');
} else if (srcValue === '/components/SizeSelector/img/size-medium.svg') {
itemSrc.value = '/components/SizeSelector/img/size-medium-active.svg';
element.setAttribute('aria-selected', 'true');
} else if (srcValue === '/components/SizeSelector/img/size-large.svg') {
itemSrc.value = '/components/SizeSelector/img/size-large-active.svg';
element.setAttribute('aria-selected', 'true');
}
},
},
{},
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
{},

);
113 changes: 43 additions & 70 deletions src/components/SizeSelector/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@
.selector-wrapper {
container-type: inline-size;

img,
h3,
p {
&__title {
font-family: font.$primaryFont;
font-weight: font.$semiBold;
font-size: font.$sm;
color: colors.$gray800;
padding-top: 1.6rem;
margin: 0;
}

&__weight {
font-family: font.$tertiaryFont;
font-weight: font.$regular;
font-size: font.$xs;
color: colors.$gray600;
padding-top: 0.6rem;
margin: 0;
}

&__image {
width: 25px;
height: 25px;
background: colors.$grey150;
border-radius: 50%;
display: block;
margin: 0 auto;
padding: 1rem;
}

&__slide {
display: flex;
justify-content: center;
Expand Down Expand Up @@ -37,64 +59,37 @@

border: 3px solid transparent;

&:first-child img {
width: 10px;
height: 10px;
padding: 2rem;
}

&:hover {
cursor: pointer;
}

&.active {
border: 3px solid colors.$blue500;

transition: 0.2s ease-in-out;
transition-timing-function: cubic-bezier(0.2, 0, 0, 1);

animation: scale 0.2s linear;
animation-fill-mode: forwards;

& img {
background: colors.$secondary100;
}
&:first-child .selector-wrapper__image {
width: 20px;
height: 20px;
}
}

h3,
img {
color: colors.$blue500;
}
&__item--active {
border: 3px solid colors.$blue500;

h3 {
font: font.$primaryFont;
}
transform: scale(1.2);
transition: 0.2s ease-in-out;

p {
font: font.$secondaryFont, font.$regular, font.$xs;
color: colors.$secondary300;
}
.selector-wrapper__image {
background: colors.$blue100;
}

h3 {
font: font.$primaryFont, font.$semiBold, font.$xs;
padding-top: 1.6rem;
.selector-wrapper__title {
font-family: font.$primaryFont;
font-weight: font.$semiBold;
font-size: font.$xs;
color: colors.$blue500;
}

p {
font: font.$secondaryFont, font.$regular, font.$xs;
padding-top: 0.6rem;
.selector-wrapper__weight {
color: colors.$blue100;
}
}

&__image {
width: 30px;
height: 30px;
background: colors.$grey150;
border-radius: 50%;
display: block;
margin: 0 auto;
padding: 1rem;
}
}

@container (max-width: 38em) {
Expand Down Expand Up @@ -131,31 +126,9 @@
gap: 2rem;
}

&__item {
width: 34%;
padding: 2.2rem 0;

&:first-child img {
width: 10px;
height: 10px;
padding: 1.4rem;
}
}

&__image {
width: 16px;
height: 16px;
}
}
}

@keyframes scale {
0% {
-webkit-transform: scale3d(1);
transform: scale(1.1);
}
100% {
-webkit-transform: scale3d(1.1);
transform: scale(1.2);
}
}
Loading