-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Options UI #135
Options UI #135
Changes from 61 commits
8403db5
9ecbfb6
522279a
388075b
abc73c7
a4a3cb8
89c82d6
3a36252
dd72202
eef6353
5a622c8
aa0d262
f32ac8f
4b31b37
99776d1
d17d94e
8d9f0c2
c6ef9e9
1bcf82c
f195797
65e0a5f
f711b1e
a96d1c5
bbbeb10
31dfdbe
117eb80
d76e6c5
b95621b
ac870d4
4d94cc8
163219a
e1fbd2c
cf98b7a
385d61b
31fc90b
bcce5a2
d83f845
d2da8d9
cef8b4c
06b2fa9
8ae382f
e962f89
5838703
5109598
ac87c64
14ee88f
6ad9d05
8768212
aa98359
0055bf2
096a4e4
8b106a8
e11122b
2ef8ddb
e7af112
78b7197
9f19f0e
cedfc75
152aad9
df537ca
da9d99a
48cc475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { h, Component } from 'preact'; | ||
import * as prettyBytes from 'pretty-bytes'; | ||
|
||
type FileContents = ArrayBuffer | Blob; | ||
|
||
interface Props extends Pick<JSX.HTMLAttributes, Exclude<keyof JSX.HTMLAttributes, 'data'>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be simpler to just rename data rather than messing with types? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that seems fine |
||
file?: FileContents; | ||
compareTo?: FileContents; | ||
increaseClass?: string; | ||
decreaseClass?: string; | ||
} | ||
|
||
interface State { | ||
size?: number; | ||
sizeFormatted?: string; | ||
compareSize?: number; | ||
compareSizeFormatted?: string; | ||
} | ||
|
||
function calculateSize(data: FileContents): number { | ||
return data instanceof ArrayBuffer ? data.byteLength : data.size; | ||
} | ||
|
||
export default class FileSize extends Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
if (props.file) { | ||
this.computeSize('size', props.file); | ||
} | ||
if (props.compareTo) { | ||
this.computeSize('compareSize', props.compareTo); | ||
} | ||
} | ||
|
||
componentWillReceiveProps({ file, compareTo }: Props) { | ||
if (file !== this.props.file) { | ||
this.computeSize('size', file); | ||
} | ||
if (compareTo !== this.props.compareTo) { | ||
this.computeSize('compareSize', compareTo); | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
this.applyStyles(); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.applyStyles(); | ||
} | ||
|
||
applyStyles() { | ||
const { size, compareSize = 0 } = this.state; | ||
if (size != null && this.base) { | ||
const delta = size && compareSize ? (size - compareSize) / compareSize : 0; | ||
this.base.style.setProperty('--size', '' + size); | ||
this.base.style.setProperty('--size-delta', '' + Math.round(Math.abs(delta * 100))); | ||
} | ||
} | ||
|
||
computeSize(prop: keyof State, data?: FileContents) { | ||
const size = data ? calculateSize(data) : 0; | ||
const pretty = prettyBytes(size); | ||
this.setState({ | ||
[prop]: size, | ||
[prop + 'Formatted']: pretty, | ||
}); | ||
} | ||
|
||
render( | ||
{ file, compareTo, increaseClass, decreaseClass, ...props }: Props, | ||
{ size, sizeFormatted = '', compareSize }: State, | ||
) { | ||
const delta = size && compareSize ? (size - compareSize) / compareSize : 0; | ||
return ( | ||
<span {...props}> | ||
{sizeFormatted} | ||
{compareTo && ( | ||
<span class={delta > 0 ? increaseClass : decreaseClass}> | ||
{delta > 0 && '+'} | ||
{Math.round(delta * 100)}% | ||
</span> | ||
)} | ||
</span> | ||
); | ||
} | ||
} |
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.
We shouldn't animate anything that involves layouts or paints unless we have no alternative. Are we currently using drop-valid and drop-invalid? I don't think we can detect that.
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.
In fact, it looks like the background-color and border-color animations are removed in cases that the background or border actually changes.
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.
Hmm - maybe try it out? Without the step animation, the overlay's "exit" animation changes to the wrong color immediately. Are stepped transitions still problematic for jank? It doesn't seem like they should be.
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.
Ahh gotcha. I misinterpreted this. We might drop the colours for drop valid/invalid (we can't properly tell what's valid or not anyay), but that's for another PR.