-
Notifications
You must be signed in to change notification settings - Fork 82
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
Fjern massa onødvendig javascript fra Collapse #1872
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
.ffe-collapse-transition { | ||
transition: height @ffe-transition-duration @ffe-ease; | ||
.ffe-collapse { | ||
display: grid; | ||
grid-template-rows: 0fr; | ||
transition: grid-template-rows var(--ffe-transition-duration) | ||
var(--ffe-ease); | ||
} | ||
|
||
.ffe-collapse--hidden { | ||
visibility: hidden; | ||
} | ||
|
||
.ffe-collapse--open { | ||
grid-template-rows: 1fr; | ||
} | ||
|
||
.ffe-collapse__inner { | ||
overflow: hidden; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,53 @@ | ||
import React, { useRef, useState, useEffect } from 'react'; | ||
import { string, bool, func, object } from 'prop-types'; | ||
import React, { useRef, useEffect, useState } from 'react'; | ||
import { string, bool, func, node } from 'prop-types'; | ||
import classNames from 'classnames'; | ||
|
||
const Collapse = ({ className, style, isOpen, onRest, ...rest }) => { | ||
const content = useRef(); | ||
const [height, setHeight] = useState(() => (isOpen ? 'auto' : '0')); | ||
const [overflow, setOverflow] = useState(() => | ||
isOpen ? 'visible' : 'hidden', | ||
); | ||
const [visibility, setVisibility] = useState(() => | ||
isOpen ? 'visible' : 'hidden', | ||
); | ||
export const Collapse = React.forwardRef( | ||
({ className, isOpen, onRest, children, ...rest }, ref) => { | ||
const internalRef = useRef(); | ||
const collapse = ref ?? internalRef; | ||
const [isHidden, setIsHidden] = useState(!isOpen); | ||
|
||
const setExpanded = () => { | ||
setHeight('auto'); | ||
setOverflow('visible'); | ||
}; | ||
|
||
const setCollapsed = () => { | ||
setVisibility('hidden'); | ||
}; | ||
|
||
useEffect(() => { | ||
if (content.current) { | ||
if (isOpen && height !== 'auto') { | ||
setHeight(`${content.current.scrollHeight}px`); | ||
setVisibility('visible'); | ||
} else if (!isOpen && height !== '0') { | ||
setHeight(`${content.current.scrollHeight}px`); | ||
window.requestAnimationFrame(() => | ||
setTimeout(() => { | ||
setHeight('0'); | ||
setOverflow('hidden'); | ||
}), | ||
); | ||
} | ||
} | ||
}, [isOpen, height]); | ||
|
||
const onTransitionEnd = e => { | ||
if (e.target === content.current && e.propertyName === 'height') { | ||
useEffect(() => { | ||
if (isOpen) { | ||
setExpanded(); | ||
} else { | ||
setCollapsed(); | ||
} | ||
if (onRest) { | ||
onRest(); | ||
setIsHidden(false); | ||
} | ||
} | ||
}; | ||
|
||
const mergedStyles = { | ||
...style, | ||
willChange: 'height', | ||
height, | ||
overflow, | ||
visibility, | ||
}; | ||
|
||
return ( | ||
<div | ||
{...rest} | ||
ref={content} | ||
style={mergedStyles} | ||
className={classNames('ffe-collapse-transition', className)} | ||
onTransitionEnd={onTransitionEnd} | ||
/> | ||
); | ||
}; | ||
}, [isOpen]); | ||
|
||
return ( | ||
<div | ||
{...rest} | ||
ref={collapse} | ||
className={classNames('ffe-collapse', className, { | ||
'ffe-collapse--open': isOpen, | ||
'ffe-collapse--hidden': isHidden, | ||
})} | ||
onTransitionEnd={e => { | ||
if ( | ||
e.target === collapse.current && | ||
e.propertyName === 'grid-template-rows' | ||
) { | ||
if (onRest) { | ||
onRest(); | ||
} | ||
if (!isOpen) { | ||
setIsHidden(true); | ||
} | ||
} | ||
}} | ||
> | ||
<div className="ffe-collapse__inner">{children}</div> | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
Collapse.displayName = 'Collapse'; | ||
|
||
Collapse.propTypes = { | ||
className: string, | ||
style: object, | ||
isOpen: bool, | ||
onRest: func, | ||
/** The content to appear when expanded */ | ||
children: node.isRequired, | ||
}; | ||
|
||
export default Collapse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import * as React from 'react'; | ||
|
||
export interface CollapseProps extends React.HTMLProps<HTMLDivElement> { | ||
export interface CollapseProps extends React.ComponentProps<'div'> { | ||
isOpen: boolean; | ||
onRest?: () => void; | ||
} | ||
|
||
declare class Collapse extends React.Component<CollapseProps, any> {} | ||
|
||
export default Collapse; | ||
export { Collapse }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default } from './Collapse'; | ||
export { Collapse } from './Collapse'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Burde kanskje beholde visibility hidden når den er lukket? Da kan ikke elementer fokuseres når den er lukket. Ser det ikke er et problem for accordion for der rendres ikke innholdet hvis lukket
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.
Det er ett veldig gott poeng. Det skall jag få fikset
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.
Har opdatert https://black-beach-0d62d0d03-1872.westeurope.2.azurestaticapps.net/