mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-14 09:39:15 +00:00
Options ui (#222)
* wip * Commenting stuff to keep the build happy * Revealing sections * Custom select elements & more form work * Range input styles * Text fields with inputs do the right thing * Safari & Firefox fixes * Large compress select * oops * MozJPEG options updated * OptPNG options * These asserts weren't true * Generic options * WebP options * Hiding "edit" when "original image" * Download icon * Copy setting button - still not happy with this * Progress indicator * Loading icon enter/exit anim * Preventing controls going under options * Ahh so that's what was causing scrolling * Ahh so that's what was causing outlines * Simplifying range styles and fixing cross-browser * Processing custom element styles * Get precision from step by default * I don't know how or when this happened. * Don't need that many steps * Avoid having an element that covers the pinch zoom * Preventing overlap with zoom controls * Prevent ts warning * Fixing spinner position * Simplifying FileSize
This commit is contained in:
92
src/components/expander/index.tsx
Normal file
92
src/components/expander/index.tsx
Normal file
@@ -0,0 +1,92 @@
|
||||
import { h, Component, ComponentChild, ComponentChildren } from 'preact';
|
||||
import * as style from './style.scss';
|
||||
import { linkRef } from '../../lib/initial-util';
|
||||
|
||||
interface Props {
|
||||
children: ComponentChildren;
|
||||
}
|
||||
interface State {
|
||||
outgoingChildren: ComponentChild[];
|
||||
}
|
||||
|
||||
export default class Expander extends Component<Props, State> {
|
||||
state: State = {
|
||||
outgoingChildren: [],
|
||||
};
|
||||
private el?: HTMLDivElement;
|
||||
private lastElHeight: number = 0;
|
||||
|
||||
componentWillReceiveProps(nextProps: Props) {
|
||||
const children = this.props.children as ComponentChild[];
|
||||
const nextChildren = nextProps.children as ComponentChild[];
|
||||
|
||||
if (!nextChildren[0] && children[0]) {
|
||||
// Cache the current children for the shrink animation.
|
||||
this.setState({ outgoingChildren: children });
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUpdate(nextProps: Props) {
|
||||
const children = this.props.children as ComponentChild[];
|
||||
const nextChildren = nextProps.children as ComponentChild[];
|
||||
|
||||
// Only interested if going from empty to not-empty, or not-empty to empty.
|
||||
if ((children[0] && nextChildren[0]) || (!children[0] && !nextChildren[0])) return;
|
||||
this.lastElHeight = this.el!.getBoundingClientRect().height;
|
||||
}
|
||||
|
||||
componentDidUpdate(previousProps: Props) {
|
||||
const children = this.props.children as ComponentChild[];
|
||||
const previousChildren = previousProps.children as ComponentChild[];
|
||||
|
||||
// Only interested if going from empty to not-empty, or not-empty to empty.
|
||||
if ((children[0] && previousChildren[0]) || (!children[0] && !previousChildren[0])) return;
|
||||
|
||||
// What height do we need to transition to?
|
||||
this.el!.style.transition = 'none';
|
||||
this.el!.style.height = '';
|
||||
const newHeight = children[0] ? this.el!.getBoundingClientRect().height : 0;
|
||||
|
||||
if (this.lastElHeight === newHeight) {
|
||||
this.el!.style.transition = '';
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the currently rendered height absolutely.
|
||||
this.el!.style.height = this.lastElHeight + 'px';
|
||||
this.el!.style.transition = '';
|
||||
this.el!.style.overflow = 'hidden';
|
||||
// Force a style calc so the browser picks up the start value.
|
||||
getComputedStyle(this.el!).height;
|
||||
// Animate to the new height.
|
||||
this.el!.style.height = newHeight + 'px';
|
||||
|
||||
const listener = () => {
|
||||
// Unset the height & overflow, so element changes do the right thing.
|
||||
this.el!.style.height = '';
|
||||
this.el!.style.overflow = '';
|
||||
this.el!.removeEventListener('transitionend', listener);
|
||||
this.el!.removeEventListener('transitioncancel', listener);
|
||||
if (this.state.outgoingChildren[0]) {
|
||||
this.setState({ outgoingChildren: [] });
|
||||
}
|
||||
};
|
||||
|
||||
this.el!.addEventListener('transitionend', listener);
|
||||
this.el!.addEventListener('transitioncancel', listener);
|
||||
}
|
||||
|
||||
render(props: Props, { outgoingChildren }: State) {
|
||||
const children = props.children as ComponentChild[];
|
||||
const childrenExiting = !children[0] && outgoingChildren[0];
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={linkRef(this, 'el')}
|
||||
class={`${style.expander} ${childrenExiting ? style.childrenExiting : ''}`}
|
||||
>
|
||||
{children[0] ? children : outgoingChildren}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user