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

WIP? Add switch component from from Patternfly 4 #767

Closed
wants to merge 16 commits into from

Conversation

wesruv
Copy link
Contributor

@wesruv wesruv commented Mar 2, 2020

#736

Testing instructions

Be sure to include detailed instructions on how your update can be tested by another developer.

Browser requirements

Your component should work in all of the following environments:

  • Latest 2 versions of Edge
  • Internet Explorer 11 (should be useable, not pixel perfect)
  • Latest 2 versions of Firefox (one on Mac OS, one of Windows OS)
  • Firefox 60.7.2 (or latest version for Red Hat Enterprise Linux distribution)
  • Latest 2 versions of Chrome (one on Mac OS, one of Windows OS)
  • Latest 2 versions of Safari
  • Galaxy S9 Firefox
  • iPhone X Safari
  • iPad Pro Safari
  • Pixel 3 Chrome

Ready-for-merge Checklist

  • Expected files: all files in this pull request are related to one feature request or issue (no stragglers)?
  • Did browser testing pass?
  • Did you update or add any necessary documentation (README.md, WHY.md, etc.)?
  • Was this feature demo'd and the design review approved?
  • Did you update the CHANGELOG.md file with a summary of this update?

Be sure to share your updates with the [email protected] mailing list!

@wesruv wesruv force-pushed the 736-add-pfe-switch-component branch from 7332f5e to 564fb2b Compare March 3, 2020 18:33
@wesruv
Copy link
Contributor Author

wesruv commented Mar 3, 2020

@castastrophe I think I got everything you noted, working on writing tests now.

BoxShadow: '0 0.0625rem 0.0625rem 0rem rgba(3, 3, 3, 0.05), 0 0.25rem 0.5rem 0.25rem rgba(3, 3, 3, 0.06)',
),
toggle: (
BackgroundColor: #d2d2d2, // @todo - didn't find a great color to match PF4
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@castastrophe couldn't find a color that made sense for this, any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

I like your todo, we can look into if we're missing a color variable from the map or if we need to update the colors in the map currently at a later date. 👍

@@ -0,0 +1,4 @@
<div class="pfe-switch">
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this div is necessary. You can style the :host, <pfe-switch>, directly in the stylesheet by using :host without needing to assign a class. If you really want the class, in the JS you could add the class to the host element too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Styling tags makes me itchy... but you're right.. old habits, something something

connectedCallback() {
super.connectedCallback();

const $pfeSwitch = this.shadowRoot.querySelector(".pfe-switch");
Copy link
Contributor

Choose a reason for hiding this comment

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

The $name syntax is a jQuery convention to indicate a jQuery element variable but would not make sense in this context since we do not use jQuery in the project. The convention we've been using for shadow elements is this._pfeSwitch = this.shadowRoot.querySelector(".pfe-switch");. If you remove the div from the template, you can reference the host simply with this and there's no need to get the shadow element at all.

"title": "Attributes",
"type": "object",
"properties": {
"form": {
Copy link
Contributor

Choose a reason for hiding this comment

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

The attributes section should list out all the data attributes supported by your component: message-on, message-off, hide-label. The form indication could go in your slot array above instead of referencing a raw field, you could indicate form to tell the component that it only accepts a form element in the slot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there an example or existing component I can code-monkey off of?

const $pfeSwitch = this.shadowRoot.querySelector(".pfe-switch");

// Get settings from attributes
const settingAttributes = [
Copy link
Contributor

Choose a reason for hiding this comment

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

This is done for you by importing the schema. Any attributes defined in your schema will be attached to your component for you by logic built into pfelement. You can access that information using this.props["message-on"] which will list out the object for that attribute. Each attribute has title, type, enum (if defined), default (if defined), a boolean prefixed value (does it have pfe at the beginning of it), and the actual value assigned (if the user has added that attribute to the component).

// Check for label text
if (!this.label.textContent.trim().length) {
console.error(
"pfe-switch: Label text not found. Form elements require meaningful label text for accessibility. The label can be visually hidden, but must exist."
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
"pfe-switch: Label text not found. Form elements require meaningful label text for accessibility. The label can be visually hidden, but must exist."
`${this.tag}: Label text not found. Form elements require meaningful label text for accessibility. The label can be visually hidden, but must exist.`

Using the variable helps in case the component is renamed.

if (this.settings["hide-label"]) {
this.label.classList.add("pfe-sr-only");
const switchIcon = document.createElement("span");
switchIcon.classList.add("pfe-switch__checkmark");
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
switchIcon.classList.add("pfe-switch__checkmark");
switchIcon.classList.add(`${this.tag}__checkmark`);

Suggest using the variable instead of hardcoding the component name.

this.checkbox.addEventListener("change", this._updateSwitchState);
}

// disconnectedCallback() {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Any event listeners you've attached above should be disconnected here.

@castastrophe
Copy link
Contributor

Link to the preview: https://deploy-preview-767--happy-galileo-ea79c4.netlify.com/elements/pfe-switch/demo/

Man this is a gorgeous component!

Screen Shot 2020-03-04 at 10 54 35 AM

}

.pfe-switch__checkmark {
// Checkmark is built with CSS instead of using an image
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool!

@wesruv wesruv force-pushed the 736-add-pfe-switch-component branch from 8f58f1a to ed41624 Compare March 12, 2020 17:55
@wesruv wesruv changed the title WIP: Add switch component from from Patternfly 4 Add switch component from from Patternfly 4 Mar 17, 2020
@wesruv wesruv changed the title Add switch component from from Patternfly 4 WIP? Add switch component from from Patternfly 4 Mar 17, 2020
cursor: pointer;

// Toggle container
&:before {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately, this type of selector isn't supported in Safari.
::before and ::after selectors inside ::slotted don't work.
WICG/webcomponents#655

image

Copy link
Contributor

Choose a reason for hiding this comment

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

We'll need to come up with an alternative approach. I'm going to investigate moving light DOM content to the shadow DOM.

:before and :after pseudo selectors don't work on slotted content in Safari. With this change, I move the knob and knob container to the shadow root and apply the same css to get the same look as before
assignedElements is not supported in IE11. Switching to assignedNodes gets this working in IE11
Using a setter to react to changes the checked value. Also, I added something I'm not quite sure about. I don't like how the checked attribute on the input doesn't stay in sync with the checked value. I know the docs reflect this on MDN (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#attr-checked) but it still bugs me.
Whenever you change the value of a CSS variable, IE11 will always use the last value that was set which is, of course, problematic. This hardcodes some values for IE11 so it's at least legible.
@castastrophe castastrophe mentioned this pull request Apr 9, 2020
kylebuch8 and others added 5 commits April 9, 2020 09:47
:before and :after pseudo selectors don't work on slotted content in Safari. With this change, I move the knob and knob container to the shadow root and apply the same css to get the same look as before
assignedElements is not supported in IE11. Switching to assignedNodes gets this working in IE11
Using a setter to react to changes the checked value. Also, I added something I'm not quite sure about. I don't like how the checked attribute on the input doesn't stay in sync with the checked value. I know the docs reflect this on MDN (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#attr-checked) but it still bugs me.
Whenever you change the value of a CSS variable, IE11 will always use the last value that was set which is, of course, problematic. This hardcodes some values for IE11 so it's at least legible.
@starryeyez024 starryeyez024 added the work in progress POC / Not ready for review label Jun 19, 2020
@castastrophe castastrophe marked this pull request as draft July 22, 2020 15:25
@starryeyez024 starryeyez024 linked an issue Sep 30, 2020 that may be closed by this pull request
@castastrophe
Copy link
Contributor

I'm going to close this out until we have time to revisit this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
work in progress POC / Not ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[NEW] pfe-switch: Create a switch component
4 participants