Last active: 2 years ago
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list.iter() {
if item > largest {
largest = item
}
}
largest
}
Last active: 2 years ago
const overlaps = (elementA: Element, elementB: Element) => {
const rectA = elementA.getBoundingClientRect();
const rectB = elementB.getBoundingClientRect();
return !(
rectA.right < rectB.left ||
rectA.left > rectB.right ||
rectA.bottom < rectB.top ||
rectA.top > rectB.bottom
);
};
Last active: 2 years ago
import clsx from 'clsx';
import { ButtonHTMLAttributes, DetailedHTMLProps, memo } from 'react';
export type ButtonProps = {
children: React.ReactNode;
} & DetailedHTMLProps<
ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
>;
const Button = ({ children, ...rest }: ButtonProps) => {
const { className, ...props } = rest;
return (
<>
<button
className={clsx(
'bg-white border border-transparent hover:border-gray-200',
'outline-none hover:bg-gray-50 focus:ring-4 dark:border-transparent',
'focus:ring-cyan-200 font-medium rounded-lg text-sm',
Last active: 2 years ago
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: 'class',
content: [
'./pages/**/*.{js,ts,jsx,tsx,md,mdx}',
'./components/**/*.{js,ts,jsx,tsx}',
'./layouts/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {
fontFamily: {
Aleo: ['Aleo', 'serif'],
'Aref-Ruqaa': ['Aref Ruqaa', 'serif'],
Barlow: ['Barlow', 'sans-serif'],
Poppins: ['Poppins', 'sans-serif'],
Mono: ['JetBrains Mono', '-apple-system', 'monospace'],
},
colors: {
bluish: {
gray: 'rgba(245,247,250)',
Last active: 2 years ago
fn main() {
let arr: [String; 6] = core::array::from_fn(|i| format!("Arr {}", i));
println!("{:?}", arr);
}
Last active: 2 years ago
Prevent overscroll/bounce in iOS
overflow: hidden;
position: relative;
overscroll-behavior: none;
Last active: 2 years ago
/**
* Swap page x and y when screen orientation is portrait.
* Method:
* pageX = pageY
* pageY = -pageX
* @param e
* @returns {[{pageY, pageX}]}
*/
getTouches(e) {
const isPortrait = window.matchMedia('(orientation: portrait)').matches;
const touches = [
{
pageX: e.touches[0].pageX,
pageY: e.touches[0].pageY,
},
];
if (isPortrait) {
[touches[0].pageX, touches[0].pageY] = [
touches[0].pageY,
-touches[0].pageX,
Last active: 2 years ago
export default function App() {
const [user, setUser] = useReducer(
(state: User, newState: Partial<User>) => ({ ...state, ...newState }),
defaultUser
);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<div>
<span>Update name: </span>
<input
type="text"
value={user.name}
onChange={(e) => setUser({ name: e.target.value })}
/>
</div>
<pre>{JSON.stringify(user, null, 2)}</pre>
Last active: 2 years ago
Use useImpertiveHandle hook to share child property to parent.
import { css } from "@emotion/css";
import { forwardRef, useImperativeHandle, useRef } from "react";
type Props = {
placeholder?: string;
};
export type ImpertiveHandler = {
focus: () => void;
};
const Child = forwardRef<ImpertiveHandler, Props>(({ placeholder }, ref) => {
const inputRef = useRef<HTMLInputElement>(null);
useImperativeHandle(ref, () => ({
focus: () => {
inputRef.current?.focus();
}
}));
return (
Last active: 2 years ago
Use useImpertiveHandle hook to share child property to parent.
import "./styles.css";
import Child, { ImpertiveHandler } from "./Child";
import { css } from "@emotion/css";
import { useRef } from "react";
export default function App() {
const inputRef = useRef<ImpertiveHandler>(null);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<div
className={css`
display: flex;
justify-content: center;
flex-flow: column;
align-items: center;
`}
>
Last active: 2 years ago
import { configWeixinService } from '@/store/helper';
import wx from '@tybys/jweixin';
import { useBoolean } from 'ahooks';
import { message } from 'antd';
import { useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
/**
* 跳转到小程序组件
*
* 利用 absolute 定位将开放标签浮动在目标元素上,
* 以实现点击时触发开放标签。
*
* [微信开放标签文档](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#%E8%B7%B3%E8%BD%AC%E5%B0%8F%E7%A8%8B%E5%BA%8F%EF%BC%9Awx-open-launch-weapp)
* @returns
*/
const OpenMiniprog = ({ appId }: { appId: string | undefined }) => {
const wrapperRef = useRef<HTMLDivElement>(null);
const wxRef = useRef<HTMLDivElement>(null);