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

Modernize polaris-color-picker components #493

Merged
merged 8 commits into from
Feb 10, 2020

Conversation

tomnez
Copy link
Contributor

@tomnez tomnez commented Feb 5, 2020

Updates PolarisColorPicker to tagless component, ES6 classes, and angle bracket syntax.

A lot of changes are just codemod changes - additional edits to the component worth pointing out are:

  • Use of {{did-insert}} and {{will-destroy}} modifiers so we can set a unique ID on the tagless component since the component files made use of this.element.
  • Added a get element() computed property to return the container element, if we think this will be confusing we could rename to something specific per component, i.e. get alphaPickerElement()
  • Added template-lint-disable no-invalid-interactive to the slider because it makes use of onmousedown and ontouchstart events.

Dummy app demo:

picker

@tomnez tomnez self-assigned this Feb 5, 2020
Copy link
Member

@vladucu vladucu left a comment

Choose a reason for hiding this comment

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

@tomnez seems some tests are failing....can you please re-tag me once they pass

@tomnez tomnez force-pushed the refactor/es6-polaris-color-picker branch 2 times, most recently from 7330824 to f7fa53d Compare February 5, 2020 20:16
@tomnez tomnez force-pushed the refactor/es6-polaris-color-picker branch from f7fa53d to 1c58f5f Compare February 5, 2020 21:06
Copy link
Member

@andrewpye andrewpye left a comment

Choose a reason for hiding this comment

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

Generally looking good @tomnez, but for whatever reason there's an iffy error on CI 😭

}

@computed('contentId')
get element() {
Copy link
Member

Choose a reason for hiding this comment

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

Curious what @vladucu's thoughts are about this naming, mostly because I'm conflicted: part of me feels like we shouldn't be overwriting Ember's internal API, part of me feels like we should be able to overwrite because that's why we do inheritance. Basically I'm good with this as is but would also understand if we'd prefer to name it something else 😝

Copy link
Member

Choose a reason for hiding this comment

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

hmm, tbh...I'm not sure I understand why we need this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vladucu you're confused as to why it's a CP, or confused as to why we need to set the element?

I guess we could just set it in the init hook since that's the only place it's used. I was just trying to keep the same API that we were used to (having the use of this.element) but I can get rid of it. If we ever end up needing it in multiple methods then we can refactor to a CP again.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, why we need the CP....we get the element explicitly as first parameter in the action called through did-insert modifier....we could just set it here as a property on the component....but unless I'm wrong, it's only used in this one place

Copy link
Contributor Author

Choose a reason for hiding this comment

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

update - was going through other files, and components like polaris-color-picker/slidable call this.element from multiple places, so maybe it's easiest to have it as a CP in this particular case?

If not we can call document.querySelector('#${this.contentId}') each time. Pretty much the same thing IMO 🤷‍♂

Copy link
Member

Choose a reason for hiding this comment

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

note: maybe I'm missing something and just talking non-sense...feel free to correct me
what I mean is....we can just keep it simple....why add a CP when you already have an action that set's contentId and then have the CP compute what you call element now

even more...could this just be? Not sure where the content stuff comes from in this PR....

/**
 * Polaris color picker component.
 * See https://polaris.shopify.com/components/forms/color-picker
 */
@tagName('')
@templateLayout(layout)
export default class PolarisColorPicker extends Component {
  ....

// NO LONGER NEEDED
//  didRender() {
//    super.didRender(...arguments);

//    let element = this.element;

//    if (isNone(element)) {
//      return;
//    }

    // Grab the size of the picker for positioning the draggable markers.
//    const mainColorElement = element.querySelector(
//      'div.Polaris-ColorPicker__MainColor'
//    );
//    if (isNone(mainColorElement)) {
//      return;
//    }

//    this.set('pickerSize', mainColorElement.clientWidth);
//  }

  @action
  setPickerSize(element) {
    // Grab the size of the picker for positioning the draggable markers.
    const mainColorElement = element.querySelector(
      'div.Polaris-ColorPicker__MainColor'
    );
    this.set('pickerSize', mainColorElement.clientWidth);
  }

  ...
}
<div
  id={{this.contentId}}
  class="Polaris-ColorPicker"
  {{did-insert this.setContentId}}
>
  <div class="Polaris-ColorPicker__MainColor">
    ...
  </div>
  ....
</div>

Copy link
Contributor Author

@tomnez tomnez Feb 6, 2020

Choose a reason for hiding this comment

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

^ Agreed, this works for polaris-color-picker, but other components will have multiple methods that need to access the container element, so in other components we should at least set element as a property on did-insert (see slidable.js, it accessing element in other places besides the did-insert method).

So I figure I'll refactor where I can, but some components might still have the element property set.

Copy link
Member

@vladucu vladucu Feb 6, 2020

Choose a reason for hiding this comment

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

don't get me wrong Tom....in the same action you call from did-insert, of course, keep a reference to the mainColorElement or whatever you need by setting it on the component....if you need that for later use too

}

@action
setContentId(element, [value]) {
Copy link
Member

Choose a reason for hiding this comment

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

Reckon this, then contentId property etc. are worth putting into a mixin? We're already using them 4 times in this PR alone 🤔

Copy link
Member

Choose a reason for hiding this comment

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

we should probably stop using mixins going further, if we want to be able to use Glimmer components
what do you think about looking later into drying this up?

addon/components/polaris-color-picker.js Show resolved Hide resolved
Copy link
Member

@vladucu vladucu left a comment

Choose a reason for hiding this comment

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

Looking good so far...left a few comments though

}

@action
setContentId(element, [value]) {
Copy link
Member

Choose a reason for hiding this comment

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

we should probably stop using mixins going further, if we want to be able to use Glimmer components
what do you think about looking later into drying this up?

addon/components/polaris-color-picker.js Show resolved Hide resolved
}

@computed('contentId')
get element() {
Copy link
Member

Choose a reason for hiding this comment

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

hmm, tbh...I'm not sure I understand why we need this


didRender() {
this._super(...arguments);
super.didRender(...arguments);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need didRender anymore...actually we want to stop using any other lifecycle hooks except init/destroy....instead we should have this logic inside the action you added setContentId (renamed ofc)

pickerSize,
color: { hue, alpha = 1 },
onChange,
} = this.getProperties('pickerSize', 'color', 'onChange');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} = this.getProperties('pickerSize', 'color', 'onChange');
} = this;

const {
color: { brightness, saturation, alpha = 1 },
onChange,
} = this.getProperties('color', 'onChange');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} = this.getProperties('color', 'onChange');
} = this;

const {
color: { hue, brightness, saturation },
onChange,
} = this.getProperties('color', 'onChange');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} = this.getProperties('color', 'onChange');
} = this;

@@ -1,6 +1,8 @@
import { tagName, layout as templateLayout } from '@ember-decorators/component';
Copy link
Member

Choose a reason for hiding this comment

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

same here (and other files), can we order this please

}

@computed('contentId')
get element() {
Copy link
Member

Choose a reason for hiding this comment

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

similar here like in main component


@action
handleChange({ y }) {
const { sliderHeight, onChange } = this.getProperties(
Copy link
Member

Choose a reason for hiding this comment

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

let's use ES6 destructuring

@tomnez
Copy link
Contributor Author

tomnez commented Feb 6, 2020

@vladucu @andrewpye updated - also, I ran the base branch locally and ember-beta and ember-canary are having issues there as well. What are your thoughts on just removing those from our ember-try config while we get the rest of these components updated?

Seems like it would allow us to move a lot faster rather than finding failing tests and skipping them.

}

@action
setContentId(element, value) {
Copy link
Member

Choose a reason for hiding this comment

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

is this copied from somewhere? I don't understand where it's coming from, can't see it in the version prior to these changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's slighty different than the old version, it used to be

setContentId(element, [value])

because it used to get called from the on-insert modifier, but now it gets called from within setContentIdAndTriggerHeightChanged below.

Copy link
Member

Choose a reason for hiding this comment

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

would you mind linking me to the old version here on github?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's here in my first commit - this link doesn't seem to scroll to the line changes, but its slidable.js, lines 150-153 - 0c66046#diff-160b5cb0d5fd7505911f238ca1295ee4R150-R153

Copy link
Member

Choose a reason for hiding this comment

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

kk....do you think this is needed? why are we adding an ID attirbute to the new wrapping div in the template that was added after making this component tag-less?

Copy link
Member

Choose a reason for hiding this comment

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

this should be much simpler....please check ember-render-modifiers to understand it's usage....it's super simple: did-insert will invoke your action with one param element (the element on which did-insert is added) and whatever else you're passing to it.....just set this element argument you receive in your action and set it on this component

there's no need guidFor and appending content....etc (which is why I was confused)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh I did that originally, but got the error

Cannot set property element of [object Object] which has only a getter

I thought maybe it was something weird with the modifiers but it turns out it just doesn't want me using the key name "element".. 🤦‍♂ So yeah I can just do that.

Copy link
Member

Choose a reason for hiding this comment

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

welp, element is a reserved word in ember so makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, strange that it let me overwrite it with a CP tho.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, strange that it let me overwrite it with a CP tho.

I guess overwriting replaces the entire setter/getter combo, while setting tries to access a non-existent setter on the existing element property 🤷‍♂

@tomnez tomnez requested a review from vladucu February 6, 2020 21:33
Copy link
Member

@vladucu vladucu left a comment

Choose a reason for hiding this comment

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

Not sure if you've missed to push the commit, but I still see in slidable things we discussed yesterday to change...can you please take a look and re-tag once ready?

@tomnez
Copy link
Contributor Author

tomnez commented Feb 7, 2020

Ahhh yeah forgot to push 🤦‍♂

@tomnez tomnez requested a review from vladucu February 7, 2020 14:55
@sivakumar-kailasam
Copy link
Contributor

classic tom 😄

@tomnez
Copy link
Contributor Author

tomnez commented Feb 7, 2020

Rough week

Copy link
Member

@vladucu vladucu left a comment

Choose a reason for hiding this comment

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

Thanks for the updates, Tom!
👍

</div>
<div
class="Polaris-ColorPicker"
{{did-insert this.setPickerSize}}
Copy link
Member

Choose a reason for hiding this comment

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

nit: let's add ...attributes here too

@tomnez tomnez merged commit db1b6f9 into refactor/es6-classes Feb 10, 2020
@tomnez tomnez deleted the refactor/es6-polaris-color-picker branch February 10, 2020 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants