Files
3x-ui/frontend/src/components/feedback/TextModal.tsx
T
Alireza Ahmadi e2184777a0 fix(frontend): make the panel usable with screen readers (#6907)
* fix(frontend): make the panel usable with screen readers

* fix(frontend): keep mobile tab names and the inbound expiry hint

Review of #6907 found two gaps in the screen-reader pass:

- On phone-width screens catTabLabel renders only the icon and names the
  tab by putting aria-label on it, but callers now pass that icon with
  aria-hidden for the desktop label, so every Settings, Xray and Host
  category tab lost its name under TalkBack. catTabLabel now clears
  aria-hidden on the icon it uses as the name.
- DateTimePicker forwarded id and aria-label but not aria-describedby,
  so the inbound expiry hint ("leave blank to never expire") was never
  attached. It now forwards it on both the Gregorian and Jalali input.
2026-10-23 18:29:47 +02:00

108 lines
2.8 KiB
TypeScript

import { useCallback, useState } from 'react';
import { Button, Input, Modal, Tabs, message } from 'antd';
import { CopyOutlined, DownloadOutlined } from '@ant-design/icons';
import { useTranslation } from 'react-i18next';
import JsonEditor from '@/components/form/JsonEditor';
import { ClipboardManager, FileManager } from '@/utils';
export interface TextModalTab {
key: string;
label: string;
content: string;
}
interface TextModalProps {
open: boolean;
onClose: () => void;
title: string;
content: string;
fileName?: string;
json?: boolean;
tabs?: TextModalTab[];
}
export default function TextModal({
open,
onClose,
title,
content,
fileName = '',
json = false,
tabs,
}: TextModalProps) {
const { t } = useTranslation();
const [messageApi, messageContextHolder] = message.useMessage();
const [activeKey, setActiveKey] = useState('');
// Reset on the way out so the next open starts on the first tab; activeTab
// falls back to tabs[0] whenever activeKey no longer matches.
const close = useCallback(() => {
setActiveKey('');
onClose();
}, [onClose]);
const activeTab = tabs?.find((tab) => tab.key === activeKey) ?? tabs?.[0];
const activeContent = activeTab ? activeTab.content : content;
async function copy() {
const ok = await ClipboardManager.copyText(activeContent || '');
if (ok) {
messageApi.success(t('copied'));
close();
}
}
function download() {
if (!fileName) return;
FileManager.downloadTextFile(activeContent, fileName);
}
return (
<>
{messageContextHolder}
<Modal
open={open}
title={title}
onCancel={close}
destroyOnHidden
footer={
<>
{fileName && (
<Button icon={<DownloadOutlined aria-hidden />} onClick={download}>
{fileName}
</Button>
)}
<Button type="primary" icon={<CopyOutlined aria-hidden />} onClick={copy}>
{t('copy')}
</Button>
</>
}
>
{tabs && tabs.length > 0 && (
<Tabs
activeKey={activeTab?.key}
onChange={setActiveKey}
items={tabs.map((tab) => ({ key: tab.key, label: tab.label }))}
/>
)}
{json ? (
<JsonEditor value={activeContent} readOnly minHeight="240px" maxHeight="60vh" />
) : (
<Input.TextArea
aria-label={title}
value={activeContent}
readOnly
autoSize={{ minRows: 10, maxRows: 20 }}
style={{
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
fontSize: 12,
overflowY: 'auto',
}}
/>
)}
</Modal>
</>
);
}