-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Conversation
@@ -48,6 +48,13 @@ const CHILD_STYLE = { | |||
position: 'relative', | |||
}; | |||
|
|||
const SECTION_HEADER_STYLE = { | |||
cursor: 'pointer', | |||
backgroundColor: '#efefef', |
There was a problem hiding this comment.
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 = {}, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
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): ...
.
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
Latest revision includes:
|
There was a problem hiding this 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 = { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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!
There was a problem hiding this 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
-
Attach the component DOM node to its own
ownerDocument.defaultView
(see lines 115 - 117). However, I don't think thestyle
tag from JSS is installed here, so not getting the styles as expected. -
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 apparentlyundefined
.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
}; | ||
|
||
// TODO(#30445): update these styles after team agrees on styling | ||
// or delete is not used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// or delete is not used | |
// or delete if not used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!
headerClassName, | ||
contentClassName, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
…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
Partial #30445.
Having a problem with rebasing on the previous PR (#30593), so created a new PR instead.
Porting CSS over from
0.1
.header
andcontent
sections.