Files
ThreadNet-Web/src/components/views/elements/Spoiler.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.7 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2019 Sorunome
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
*/
2019-05-22 20:41:27 +02:00
import React from "react";
2021-10-22 17:23:32 -05:00
2021-09-15 20:47:49 +02:00
interface IProps {
reason?: string;
contentHtml: string;
}
interface IState {
visible: boolean;
}
export default class Spoiler extends React.Component<IProps, IState> {
public constructor(props: IProps) {
super(props);
this.state = {
2019-05-22 20:41:27 +02:00
visible: false,
};
}
2019-05-22 20:41:27 +02:00
2021-09-15 20:47:49 +02:00
private toggleVisible = (e: React.MouseEvent): void => {
if (!this.state.visible) {
// we are un-blurring, we don't want this click to propagate to potential child pills
e.preventDefault();
e.stopPropagation();
}
2019-05-22 20:41:27 +02:00
this.setState({ visible: !this.state.visible });
2021-09-15 20:47:49 +02:00
};
2019-05-22 20:41:27 +02:00
public render(): React.ReactNode {
2019-05-22 20:41:27 +02:00
const reason = this.props.reason ? (
<span className="mx_EventTile_spoiler_reason">{"(" + this.props.reason + ")"}</span>
2019-05-22 20:41:27 +02:00
) : null;
// react doesn't allow appending a DOM node as child.
// as such, we pass the this.props.contentHtml instead and then set the raw
// HTML content. This is secure as the contents have already been parsed previously
2019-05-22 20:41:27 +02:00
return (
<button
2021-09-15 20:47:49 +02:00
className={"mx_EventTile_spoiler" + (this.state.visible ? " visible" : "")}
onClick={this.toggleVisible}
>
2019-05-22 20:41:27 +02:00
{reason}
&nbsp;
2019-06-11 21:08:45 +02:00
<span
className="mx_EventTile_spoiler_content"
dangerouslySetInnerHTML={{ __html: this.props.contentHtml }}
/>
</button>
2019-05-22 20:41:27 +02:00
);
}
}