Last active: 3 years ago
Collapse with css property max height
const PostTOC = ({ headings }: Props) => {
const [show, setShow] = useState(false);
const handleClick = useCallback(() => setShow((show) => !show), []);
return (
<>
<div
className={classNames(
'rounded-lg transition-all',
'duration-500 overflow-hidden'
)}
// Max height must be seted, max-content doesn't work
style={{
maxHeight: show ? (headings?.length ?? 0) * 50 + 70 : 70,
}}
>
<h2
className={classNames(
styles.head,
'bg-white !m-[unset] p-4',
Last active: 3 years ago
Build your own tab component with tailwind css
import classNames from 'classnames';
import React, { useState } from 'react';
import { useCallback } from 'react';
type Props = {
defaultValue: string | number;
children: React.ReactElement<ItemProps>[];
};
const Tab = ({ defaultValue, children }: Props) => {
const [currentValue, setCurrentValue] = useState(defaultValue);
const handleSwitch = useCallback((value: ItemProps['value']) => {
setCurrentValue(value);
}, []);
// Pass current selected state to child
const childrenWithProps = React.Children.map(children, (child) => {
if (React.isValidElement(child)) {
return React.cloneElement(child, {
showContent: child.props.value === currentValue,
Last active: 3 years ago
Build your own tab component with tailwind css
type ItemProps = {
value: string | number;
label: string | number;
showContent?: boolean;
children?: React.ReactNode;
};
Tab.Item = function TabItem({ showContent, children }: ItemProps) {
return (
<>
<div className={classNames('hidden', showContent && '!block')}>
{children}
</div>
</>
);
};
Last active: 3 years ago
Use mouse wheel scroll horizontally for react-custom-scrollbars-2
<Scrollbars ref={scrollRef} onWheel={handleWheel} autoHeight>
<div
className={`${style['tab-wrapper']} container`}
style={{ justifyContent: 'unset', padding: '7px 0' }}
>
{list.map((item, i) => (
<ScenesTranslateItem key={i} item={item} index={i} />
))}
</div>
</Scrollbars>;
Last active: 3 years ago
Use mouse wheel scroll horizontally for react-custom-scrollbars-2
import { useEffect, useRef } from 'react';
import Scrollbars from 'react-custom-scrollbars-2';
const useWheelScroll = () => {
// innerWidth - aside - margin - padding
const editorWidth = window.innerWidth - 378 - 48;
// ScrollBars
const scrollRef = useRef<Scrollbars>(null);
// 滚动的距离
const scrollLeft = useRef(0);
// ScrollBars 可滚动的 container
const scrollWrapper = useRef<Element>();
// 可滚动的最大距离
const maxScrollWidth = useRef(0);
/**
* 保存 ScrollBars 可滚动的 container 到 ref 中
* 再监听滚动事件时,横向滚动 container
*/
useEffect(() => {
Last active: 3 years ago
Restricted polymorphism
function call<T extends unknown[], R>(fn: (...args: T) => R, ...args: T): R {
return fn(...args);
}
function fill(length: number, value: string): string[] {
return new Array(length).fill(value);
}
const test = call(fill, 10, 'xfy');
console.log(test);
Last active: 3 years ago
Detect whether opened page with WeChat
const isWeChat = () => /MicroMessenger/i.test(window.navigator.userAgent);
Last active: 3 years ago
proxy for wsl2
# 主机 IP 保存在 /etc/resolv.conf 中
export host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")
# wget 比较特殊,不认 all_proxy,只认 http_proxy 和 https_proxy
alias proxy="export all_proxy=http://$host_ip:10809 http_proxy=http://$host_ip:10809 https_proxy=http://$host_ip:10809"
alias unproxy='unset all_proxy http_proxy https_proxy'
alias winip='cat /etc/resolv.conf|grep nameserver|awk "{print $2}"'
source /home/xfy/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
. ~/proxy.sh
set_proxy
Last active: 3 years ago
proxy for wsl2
HOST_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
WSL_IP=$(hostname -I | awk '{print $1}')
PROXY_PORT=10809
PROXY_HTTP="http://${HOST_IP}:${PROXY_PORT}"
set_proxy(){
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"
export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"
export ALL_PROXY="${PROXY_SOCKS5}"
export all_proxy=${PROXY_SOCKS5}
git config --global http.proxy ${PROXY_HTTP}
git config --global https.proxy ${PROXY_HTTP}
# git ssh proxy
Last active: 3 years ago
This is JavaScript
class ParentA {
constructor() {
this.id = 'a';
}
foo() {
console.log('Parent A: ', this.id);
}
}
class ParentB {
constructor() {
this.id = 'b';
}
foo() {
console.log('Parent B: ', this.id);
}
}
class ChildA extends ParentA {
foo() {
Last active: 3 years ago
迭代器循环
const arr = [1, 2, 3, 'x', 'f', 'y'];
// for (const i of arr) {
// console.log(i);
// }
const it = arr[Symbol.iterator]();
for (let res; (res = it.next()) && !res.done; ) {
console.log(res.value);
}
Last active: 3 years ago
Number.valueOf
let x = 2;
Number.prototype.valueOf = function () {
return x++;
};
const a = new Number(2);
console.log(a == 2 && a == 3 && a == 4);
Last active: 3 years ago
数组去重合并
let arr = [
{ name: 'zs', age: 15, happy: ['唱歌', '玩游戏'] },
{ name: 'ls', age: 15, happy: ['玩游戏'] },
{ name: 'zs', age: 15, happy: ['学习', '划水'] },
];
function test(arr) {
let _obj = {};
let _arr = [];
if (!arr.length) return;
arr.map((item) => {
_obj[item.name]?.happy.length
? (_obj[item.name].happy = [..._obj[item.name].happy, ...item.happy])
: (_obj[item.name] = item);
});
_arr = Object.values(_obj);
return _arr;
}
let arr2 = test(arr);
Last active: 3 years ago
数组去重合并
let arr = [
{ name: 'zs', age: 15, happy: ['唱歌', '玩游戏'] },
{ name: 'ls', age: 15, happy: ['玩游戏'] },
{ name: 'zs', age: 15, happy: ['学习', '划水'] },
];
function test(arr) {
let _obj = {};
let _arr = [];
if (arr.length)
arr.map((item) => {
_obj[item.name]?.happy.length
? (_obj[item.name].happy = _obj[item.name].happy.concat(item.happy))
: (_obj[item.name] = item);
});
_arr = Object.values(_obj);
return _arr;
}
let arr2 = test(arr);