From 0bf2d72d05f285ddcc45179c0a4f64bbc675ad4b Mon Sep 17 00:00:00 2001 From: Haishan Date: Mon, 15 Jun 2020 23:17:50 +0800 Subject: [PATCH] refactor: use grid for proxy list layout --- src/components/Root.css | 3 ++ src/components/proxies/Proxy.module.css | 35 +++++++++++++----- src/components/proxies/Proxy.tsx | 31 ++++++++++++++-- src/components/proxies/ProxyGroup.tsx | 18 ++++------ src/components/proxies/ProxyList.module.css | 25 +++++-------- src/components/proxies/ProxyList.tsx | 40 ++++++++------------- 6 files changed, 87 insertions(+), 65 deletions(-) diff --git a/src/components/Root.css b/src/components/Root.css index 8a2e828..a31b88b 100644 --- a/src/components/Root.css +++ b/src/components/Root.css @@ -97,6 +97,9 @@ body { Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, 'PingFang SC', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif; + -webkit-tap-highlight-color: rgba(0, 0, 0, 0); + -webkit-text-size-adjust: 100%; + -webkit-font-smoothing: antialiased; margin: 0; padding: 0; } diff --git a/src/components/proxies/Proxy.module.css b/src/components/proxies/Proxy.module.css index 089b686..a858e88 100644 --- a/src/components/proxies/Proxy.module.css +++ b/src/components/proxies/Proxy.module.css @@ -4,6 +4,10 @@ border-radius: 8px; overflow: hidden; + display: flex; + flex-direction: column; + justify-content: space-between; + max-width: 280px; @media (--breakpoint-not-small) { min-width: 200px; @@ -19,6 +23,13 @@ &.error { opacity: 0.5; } + &.selectable { + transition: transform 0.2s ease-in-out; + cursor: pointer; + &:hover { + transform: translateY(-2px); + } + } } .proxyType { @@ -37,20 +48,28 @@ .proxyName { width: 100%; - overflow: hidden; - text-overflow: ellipsis; margin-bottom: 5px; font-size: 0.85em; @media (--breakpoint-not-small) { - font-size: 1.1em; + font-size: 1em; } } .proxySmall { - .now { - outline: pink solid 1px; + width: 13px; + height: 13px; + border-radius: 50%; + border: 1px solid var(--color-background); + + &.now { + border-color: var(--color-text-secondary); + } + + &.selectable { + transition: transform 0.1s ease-in-out; + cursor: pointer; + &:hover { + transform: scale(1.2); + } } - width: 12px; - height: 12px; - border-radius: 8px; } diff --git a/src/components/proxies/Proxy.tsx b/src/components/proxies/Proxy.tsx index 50eba1c..d7aec74 100644 --- a/src/components/proxies/Proxy.tsx +++ b/src/components/proxies/Proxy.tsx @@ -41,9 +41,17 @@ type ProxyProps = { now?: boolean; proxy: any; latency: any; + isSelectable?: boolean; + onClick?: (proxyName: string) => unknown; }; -function ProxySmallImpl({ now, name, latency }: ProxyProps) { +function ProxySmallImpl({ + now, + name, + latency, + isSelectable, + onClick, +}: ProxyProps) { const color = useMemo(() => getLabelColor(latency), [latency]); const title = useMemo(() => { let ret = name; @@ -55,20 +63,37 @@ function ProxySmallImpl({ now, name, latency }: ProxyProps) { return (
{ + isSelectable && onClick && onClick(name); + }} /> ); } -function ProxyImpl({ now, name, proxy, latency }: ProxyProps) { +function ProxyImpl({ + now, + name, + proxy, + latency, + isSelectable, + onClick, +}: ProxyProps) { const color = useMemo(() => getLabelColor(latency), [latency]); return (
{ + isSelectable && onClick && onClick(name); + }} >
{name}
diff --git a/src/components/proxies/ProxyGroup.tsx b/src/components/proxies/ProxyGroup.tsx index eabf074..4dd4fac 100644 --- a/src/components/proxies/ProxyGroup.tsx +++ b/src/components/proxies/ProxyGroup.tsx @@ -16,7 +16,7 @@ import { ProxyList, ProxyListSummaryView } from './ProxyList'; import s0 from './ProxyGroup.module.css'; -const { useCallback, useMemo, useState } = React; +const { createElement, useCallback, useMemo, useState } = React; function ZapWrapper() { return ( @@ -73,16 +73,12 @@ function ProxyGroupImpl({ name, all, type, now, isOpen, apiConfig, dispatch }) {
- {isOpen ? ( - - ) : ( - - )} + {createElement(isOpen ? ProxyList : ProxyListSummaryView, { + all, + now, + isSelectable, + itemOnTapCallback, + })}
); } diff --git a/src/components/proxies/ProxyList.module.css b/src/components/proxies/ProxyList.module.css index eb38ca8..ba00f90 100644 --- a/src/components/proxies/ProxyList.module.css +++ b/src/components/proxies/ProxyList.module.css @@ -1,22 +1,13 @@ .list { - display: flex; - flex-wrap: wrap; margin-top: 8px; + display: grid; + grid-template-columns: repeat(auto-fill, minmax(auto, 280px)); + grid-gap: 10px; } -.proxy { - margin-right: 5px; - margin-bottom: 5px; - @media (--breakpoint-not-small) { - margin-right: 10px; - margin-bottom: 10px; - } - transition: transform 0.2s ease-in-out; - - &.proxySelectable { - cursor: pointer; - &:hover { - transform: translateY(-2px); - } - } +.listSummaryView { + margin-top: 8px; + display: grid; + grid-template-columns: repeat(auto-fill, 13px); + grid-gap: 10px; } diff --git a/src/components/proxies/ProxyList.tsx b/src/components/proxies/ProxyList.tsx index 6100367..850d360 100644 --- a/src/components/proxies/ProxyList.tsx +++ b/src/components/proxies/ProxyList.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; -import cx from 'clsx'; +/* import cx from 'clsx'; */ import { Proxy, ProxySmall } from './Proxy'; @@ -25,20 +25,14 @@ export function ProxyList({ return (
{proxies.map((proxyName) => { - const proxyClassName = cx(s.proxy, { - [s.proxySelectable]: isSelectable, - }); return ( -
{ - if (!isSelectable || !itemOnTapCallback) return; - itemOnTapCallback(proxyName); - }} - > - -
+ onClick={itemOnTapCallback} + isSelectable={isSelectable} + name={proxyName} + now={proxyName === now} + /> ); })}
@@ -52,22 +46,16 @@ export function ProxyListSummaryView({ itemOnTapCallback, }: ProxyListProps) { return ( -
+
{all.map((proxyName) => { - const proxyClassName = cx(s.proxy, { - [s.proxySelectable]: isSelectable, - }); return ( -
{ - if (!isSelectable || !itemOnTapCallback) return; - itemOnTapCallback(proxyName); - }} - > - -
+ onClick={itemOnTapCallback} + isSelectable={isSelectable} + name={proxyName} + now={proxyName === now} + /> ); })}