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

✨ [bento][amp-accordion] CSS Port draft for amp accordion (PR #2) #30622

Merged
merged 9 commits into from
Oct 20, 2020

Conversation

krdwan
Copy link
Contributor

@krdwan krdwan commented Oct 12, 2020

Partial #30445.

Having a problem with rebasing on the previous PR (#30593), so created a new PR instead.

Porting CSS over from 0.1.

  • Added styling for bento and amp modes for header and content sections.
  • Preact mode is controlled by JSS
  • amp mode is controlled by component css styling

@CLAassistant
Copy link

CLAassistant commented Oct 12, 2020

CLA assistant check
All committers have signed the CLA.

@google-cla google-cla bot added the cla: yes label Oct 12, 2020
@krdwan krdwan changed the title Update CSS for accordion ✨ [bento][amp-accordion] CSS Port draft for amp accordion Oct 12, 2020
@krdwan krdwan changed the title ✨ [bento][amp-accordion] CSS Port draft for amp accordion ✨ [bento][amp-accordion] CSS Port draft for amp accordion (PR #2) Oct 12, 2020
@@ -48,6 +48,13 @@ const CHILD_STYLE = {
position: 'relative',
};

const SECTION_HEADER_STYLE = {
cursor: 'pointer',
backgroundColor: '#efefef',
Copy link
Contributor

Choose a reason for hiding this comment

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

Cursor pointer is ok here. But we can't put any of the rest into the inline style - this would make them un-overridable in stylesheets. Let's remove these styles for now. If the wg decides we want these styles here, we will have to bring JSS back. But for now it's easier to not have it, imho.

@@ -152,6 +159,8 @@ export function AccordionSection({
contentAs: ContentComp = 'div',
expanded: defaultExpanded = false,
animate: defaultAnimate = false,
headerStyle = {},
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for defaults here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, removed.

}

/* heading
* TODO(dvoytenko): remove most of these styles, except maybe for `cursor`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Rewrite TODO as TODO(#30445): ....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@@ -128,6 +129,7 @@ const bindSectionShimToElement = (element) => SectionShim.bind(null, element);
*/
function HeaderShim(sectionElement, {onClick}) {
const headerElement = sectionElement.firstElementChild;
headerElement.classList.add('i-amphtml-accordion-header');
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 a side-effect. Let's move it to the useLayoutEffect below. Alternatively, we can move this code to getState.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@@ -157,6 +159,7 @@ const bindHeaderShimToElement = (element) => HeaderShim.bind(null, element);
*/
function ContentShimWithRef(sectionElement, {hidden}, ref) {
const contentElement = sectionElement.lastElementChild;
contentElement.classList.add('i-amphtml-accordion-content');
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

@krdwan
Copy link
Contributor Author

krdwan commented Oct 12, 2020

Latest revision includes:

  1. Moved styling in Preact to use JSX per team discussion.
  2. This includes CHILD_STYLING(CSS related to animations)
  3. Addressed review comments.

Copy link
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

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

Looks good, but one request.

@@ -37,17 +38,6 @@ const EMPTY_EXPANDED_MAP = {};

const generateSectionId = sequentialIdGenerator();

const CHILD_STYLE = {
Copy link
Contributor

Choose a reason for hiding this comment

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

These particular styles are not overridable. As such, we can continue to inline them. If you still want them in the JSS - it's totally reasonable, but we need to add !important.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I personally prefer using JSS to keep all styling together. I think it improves readability so the developer can quickly look into the one JSS file to see all styling. I have added !important. If there's something else I should consider, don't hesitate to let me know :). Thanks!

Copy link
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

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

LGTM with one ask for tests.

expect(content2.hidden).to.be.true;

// Styling.
expect(header0.className.includes('section-child')).to.be.true;
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO instead of testing for class names (or with them) I'd like to attach the accordion to DOM and call getComputedStyle() to check critical styles - the ones we marked with !important. Otherwise, the class presence does not guarantee that we didn't make a bug with the class definition itself.

Copy link
Contributor Author

@krdwan krdwan Oct 15, 2020

Choose a reason for hiding this comment

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

Sounds good Dima. For the test-amp-accordion.js (amp mode tests) I have made the updates.

For test-accordion.js (preact tests), I have not been able to get the tests to work. A few things I tried include:

  1. Attach the component DOM node to its own ownerDocument.defaultView (see lines 115 - 117). However, I don't think the style tag from JSS is installed here, so not getting the styles as expected.

  2. I also tried using options in the mount section (see line 109). However, I'm unable to attach the component DOM node as win.document is apparently undefined.

I'm not too familiar with the scoping in the test environments. Do you have any recommendations on where I can find out more about this?

code lines refer to latest commit: d08c4fb
(file: test-accordion.js)

Copy link
Contributor

Choose a reason for hiding this comment

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

While these tests are super thorough and help increase confidence, it's not common to test CSS properties this granularly inside the components for visual styles. If it's not too much trouble, what do you think about copying the visual tests from 0.1 over? It should give us just as much information without as much of this tedium.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! As discussed offline, I've added an item in the tracker issue to look into the visual tests separately from this PR. #30445

@krdwan krdwan marked this pull request as ready for review October 15, 2020 19:23
};

// TODO(#30445): update these styles after team agrees on styling
// or delete is not used
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
// or delete is not used
// or delete if not used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

Comment on lines 145 to 146
headerClassName,
contentClassName,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: What do you think about giving these a default value with headerClassName = '' so we do not need the || '' later on?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm fine with this, updated.

/* heading/content */
.i-amphtml-accordion-header,
.i-amphtml-accordion-content {
margin: 0;
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 actually interesting. It looks like in AMP, we don't respect margins of a given header, but in Preact we do by nature of wrapping the given header element inside another layer of <header> tag. @dvoytenko Is there a reason we did not attach the header class/handlers/etc on the cloneElement of the given header in Preact? Also why did we move away from the accordion model of "first child is header and second child is content"?

I understand these questions are not directly related to the PR so feel free to consider them non blocking here.

expect(content2.hidden).to.be.true;

// Styling.
expect(header0.className.includes('section-child')).to.be.true;
Copy link
Contributor

Choose a reason for hiding this comment

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

While these tests are super thorough and help increase confidence, it's not common to test CSS properties this granularly inside the components for visual styles. If it's not too much trouble, what do you think about copying the visual tests from 0.1 over? It should give us just as much information without as much of this tedium.

@krdwan krdwan merged commit 21d8e50 into ampproject:master Oct 20, 2020
ed-bird pushed a commit to ed-bird/amphtml that referenced this pull request Dec 10, 2020
…ect#2) (ampproject#30622)

* Update CSS for accordion

* Add JSX into Preact component styling and misc review comments

* Add important to make section styles impossible to overwrite

* Add tests for CSS in amp-accordion

* Accordion tests updates

* Removed tests for getComputedStyle

* Remove debugging comments

* Address nits from review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants