From fdf30c37cbb76e00e05d075a41a2f242742ad4b7 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Tue, 18 Apr 2023 23:33:59 +0800 Subject: [PATCH] Replace datalist with my own list --- src/pages/login.css | 29 +++++++++++++++++- src/pages/login.jsx | 71 +++++++++++++++++++++++++++++++++------------ 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/pages/login.css b/src/pages/login.css index 69cc6c28..58a4388f 100644 --- a/src/pages/login.css +++ b/src/pages/login.css @@ -22,5 +22,32 @@ #login input { display: block; - width: 100%; + width: 15em; + margin: 0 auto; + max-width: 100%; + transition: all 0.2s ease-in-out; +} + +#instances-suggestions { + margin: 0.2em 0 0; + padding: 0; + list-style: none; + width: 90vw; + max-width: 40em; + overflow: auto; + white-space: nowrap; + mask-image: linear-gradient( + to right, + transparent, + black 1.2em, + black calc(100% - 5em), + transparent + ); + animation: fade-in 0.2s ease-in-out; + height: 2.5em; +} +#instances-suggestions li { + display: inline-block; + margin: 0; + padding: 0; } diff --git a/src/pages/login.jsx b/src/pages/login.jsx index e86aa05c..a805eaee 100644 --- a/src/pages/login.jsx +++ b/src/pages/login.jsx @@ -14,6 +14,9 @@ function Login() { const instanceURLRef = useRef(); const cachedInstanceURL = store.local.get('instanceURL'); const [uiState, setUIState] = useState('default'); + const [instanceText, setInstanceText] = useState( + cachedInstanceURL?.toLowerCase() || '', + ); const [instancesList, setInstancesList] = useState([]); useEffect(() => { @@ -29,20 +32,13 @@ function Login() { })(); }, []); - useEffect(() => { - if (cachedInstanceURL) { - instanceURLRef.current.value = cachedInstanceURL.toLowerCase(); - } - }, []); + // useEffect(() => { + // if (cachedInstanceURL) { + // instanceURLRef.current.value = cachedInstanceURL.toLowerCase(); + // } + // }, []); - const onSubmit = (e) => { - e.preventDefault(); - const { elements } = e.target; - let instanceURL = elements.instanceURL.value.toLowerCase(); - // Remove protocol from instance URL - instanceURL = instanceURL.replace(/^https?:\/\//, '').replace(/\/+$/, ''); - // Remove @acct@ or acct@ from instance URL - instanceURL = instanceURL.replace(/^@?[^@]+@/, ''); + const submitInstance = (instanceURL) => { store.local.set('instanceURL', instanceURL); (async () => { @@ -71,6 +67,22 @@ function Login() { })(); }; + const onSubmit = (e) => { + e.preventDefault(); + const { elements } = e.target; + let instanceURL = elements.instanceURL.value.toLowerCase(); + // Remove protocol from instance URL + instanceURL = instanceURL.replace(/^https?:\/\//, '').replace(/\/+$/, ''); + // Remove @acct@ or acct@ from instance URL + instanceURL = instanceURL.replace(/^@?[^@]+@/, ''); + if (!/\./.test(instanceURL)) { + instanceURL = instancesList.find((instance) => + instance.includes(instanceURL), + ); + } + submitInstance(instanceURL); + }; + return (
@@ -78,34 +90,57 @@ function Login() { {uiState === 'error' && (

Failed to log in. Please try again or another instance.

)} -

+

{' '} -

+