├── mock ├── .gitkeep └── api.ts ├── src ├── pages │ ├── 404.tsx │ ├── login │ │ ├── index.less │ │ └── index.tsx │ ├── loading.tsx │ ├── details │ │ └── index.tsx │ ├── message │ │ └── index.tsx │ ├── account │ │ ├── index.less │ │ └── index.tsx │ ├── todo │ │ └── index.tsx │ └── home │ │ ├── index.less │ │ └── index.tsx ├── app.ts ├── components │ ├── icons │ │ ├── utils │ │ │ ├── canUseDom.ts │ │ │ └── dynamicCSS.ts │ │ ├── index.ts │ │ ├── Context.tsx │ │ ├── README.md │ │ ├── IconFont.tsx │ │ └── Icon.tsx │ └── index.ts ├── layouts │ ├── index.less │ ├── tab-bar │ │ ├── index.less │ │ └── index.tsx │ └── index.tsx ├── wrappers │ └── index.tsx ├── app.less └── theme.less ├── .husky ├── pre-commit └── commit-msg ├── .stylelintrc.js ├── .prettierignore ├── .prettierrc ├── typings.d.ts ├── .editorconfig ├── .gitignore ├── .lintstagedrc ├── .eslintrc.js ├── tsconfig.json ├── LICENSE.md ├── package.json ├── config ├── routes.ts └── config.ts ├── README-zh_CN.md └── README.md /mock/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/pages/404.tsx: -------------------------------------------------------------------------------- 1 | export default ()=>{ 2 | return
404
3 | } 4 | -------------------------------------------------------------------------------- /src/app.ts: -------------------------------------------------------------------------------- 1 | import 'antd-mobile/2x/es/global'; 2 | import './app.less'; 3 | import './theme.less'; 4 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | . "$(dirname -- "$0")/_/husky.sh" 3 | 4 | npx --no-install lint-staged --quiet 5 | -------------------------------------------------------------------------------- /.stylelintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // Umi Max 项目 3 | extends: require.resolve('@umijs/max/stylelint'), 4 | }; 5 | -------------------------------------------------------------------------------- /.husky/commit-msg: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | . "$(dirname -- "$0")/_/husky.sh" 3 | 4 | npx --no-install max verify-commit $1 5 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | **/*.md 2 | **/*.svg 3 | **/*.ejs 4 | **/*.html 5 | package.json 6 | .umi 7 | .umi-production 8 | .umi-test 9 | -------------------------------------------------------------------------------- /src/pages/login/index.less: -------------------------------------------------------------------------------- 1 | .login-page { 2 | &-wrap { 3 | position: relative; 4 | font-size: 14px; 5 | transform: scale(0.5); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/components/icons/utils/canUseDom.ts: -------------------------------------------------------------------------------- 1 | export default function canUseDom() { 2 | return !!( 3 | typeof window !== 'undefined' && 4 | window.document && 5 | window.document.createElement 6 | ); 7 | } 8 | -------------------------------------------------------------------------------- /src/layouts/index.less: -------------------------------------------------------------------------------- 1 | .global-layout { 2 | position: relative; 3 | width: 100%; 4 | height: 100%; 5 | 6 | .basic-layout-warp { 7 | position: relative; 8 | width: 100%; 9 | height: 100%; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all", 4 | "printWidth": 80, 5 | "overrides": [ 6 | { 7 | "files": ".prettierrc", 8 | "options": { "parser": "json" } 9 | } 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /src/components/icons/index.ts: -------------------------------------------------------------------------------- 1 | import Context from '@/components/icons/Context'; 2 | export { default as createFromIconfontCN } from './IconFont'; 3 | export { default } from '@/components/icons/Icon'; 4 | const IconProvider = Context.Provider; 5 | export { IconProvider }; 6 | -------------------------------------------------------------------------------- /src/components/index.ts: -------------------------------------------------------------------------------- 1 | import Icon, { createFromIconfontCN } from './icons'; 2 | 3 | const UmiAntdMobileIcon = createFromIconfontCN({ 4 | scriptUrl: '//at.alicdn.com/t/font_3162733_un2ldgoxap.js', // 在 iconfont.cn 上生成 5 | }); 6 | 7 | export { Icon, UmiAntdMobileIcon }; 8 | -------------------------------------------------------------------------------- /src/components/icons/Context.tsx: -------------------------------------------------------------------------------- 1 | import { createContext } from 'react'; 2 | 3 | export interface IconContextProps { 4 | prefixCls?: string; 5 | csp?: { nonce?: string }; 6 | } 7 | 8 | const IconContext = createContext({}); 9 | 10 | export default IconContext; 11 | -------------------------------------------------------------------------------- /typings.d.ts: -------------------------------------------------------------------------------- 1 | declare module '*.css'; 2 | declare module '*.less'; 3 | declare module '*.png'; 4 | declare module '*.svg' { 5 | export function ReactComponent( 6 | props: React.SVGProps, 7 | ): React.ReactElement; 8 | const url: string; 9 | export default url; 10 | } 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | [Makefile] 16 | indent_style = tab 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /npm-debug.log* 6 | /yarn-error.log 7 | /yarn.lock 8 | /package-lock.json 9 | 10 | # production 11 | /dist 12 | 13 | # misc 14 | .DS_Store 15 | 16 | # umi 17 | /src/.umi 18 | /src/.umi-production 19 | /src/.umi-test 20 | /.env.local 21 | 22 | /.idea/ 23 | -------------------------------------------------------------------------------- /.lintstagedrc: -------------------------------------------------------------------------------- 1 | { 2 | "*.{md,json}": [ 3 | "prettier --cache --write" 4 | ], 5 | "*.{js,jsx}": [ 6 | "max lint --fix --eslint-only", 7 | "prettier --cache --write" 8 | ], 9 | "*.{css,less}": [ 10 | "max lint --fix --stylelint-only", 11 | "prettier --cache --write" 12 | ], 13 | "*.ts?(x)": [ 14 | "max lint --fix --eslint-only", 15 | "prettier --cache --parser=typescript --write" 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /src/wrappers/index.tsx: -------------------------------------------------------------------------------- 1 | import { Navigate, Outlet } from '@umijs/max'; 2 | 3 | //假的权限验证 4 | const useAuth = () => { 5 | const isLogin = true; 6 | return { isLogin }; 7 | }; 8 | 9 | export default () => { 10 | const { isLogin } = useAuth(); 11 | if (isLogin) { 12 | return ( 13 |
14 | 15 |
16 | ); 17 | } else { 18 | return ; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /src/app.less: -------------------------------------------------------------------------------- 1 | html, 2 | body { 3 | margin: 0 auto; 4 | padding: 0; 5 | } 6 | 7 | html { 8 | touch-action: manipulation; 9 | } 10 | 11 | .loading-warp { 12 | display: flex; 13 | flex-direction: column; 14 | align-items: center; 15 | justify-content: center; 16 | width: 100vw; 17 | height: 100vh; 18 | background: rgba(255, 255, 255, 0.5); 19 | 20 | &-text { 21 | margin-top: 15px; 22 | color: #333333; 23 | font-size: 28px; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /mock/api.ts: -------------------------------------------------------------------------------- 1 | import mockjs from 'mockjs'; 2 | 3 | export default { 4 | // 使用 mockjs 等三方库 5 | 'GET /api/tags': mockjs.mock({ 6 | 'list|100': [{ name: '@city', 'value|1-100': 50, 'type|0-2': 1 }], 7 | }), 8 | // 支持值为 Object 和 Array 9 | 'GET /api/users': { users: [1, 2] }, 10 | 11 | // GET 可忽略 12 | '/api/users/1': { id: 1 }, 13 | 14 | // 支持自定义函数,API 参考 express@4 15 | 'POST /api/users/create': (req: any, res: any) => { 16 | // 添加跨域请求头 17 | res.setHeader('Access-Control-Allow-Origin', '*'); 18 | res.end('ok'); 19 | }, 20 | }; 21 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: require.resolve('@umijs/max/eslint'), 3 | plugins: ['react'], 4 | rules: { 5 | '@typescript-eslint/indent': 'off', 6 | '@typescript-eslint/explicit-function-return-type': 'off', 7 | '@typescript-eslint/no-unused-expressions': 'off', 8 | '@typescript-eslint/no-var-requires': 0, 9 | '@typescript-eslint/explicit-module-boundary-types': 0, 10 | 'import/no-anonymous-default-export': 0, 11 | '@typescript-eslint/no-explicit-any': 0, 12 | '@typescript-eslint/no-invalid-this': 0, 13 | 'react/no-array-index-key': 0, 14 | }, 15 | }; 16 | -------------------------------------------------------------------------------- /src/pages/loading.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { SpinLoading } from 'antd-mobile'; 3 | import '../app.less'; 4 | 5 | /** 6 | * 如何禁用掉每次刷新路由时出现的 loading... 状态? 7 | * https://umijs.org/zh-CN/docs/faq#%E5%A6%82%E4%BD%95%E7%A6%81%E7%94%A8%E6%8E%89%E6%AF%8F%E6%AC%A1%E5%88%B7%E6%96%B0%E8%B7%AF%E7%94%B1%E6%97%B6%E5%87%BA%E7%8E%B0%E7%9A%84-loading-%E7%8A%B6%E6%80%81%EF%BC%9F 8 | */ 9 | export default () => { 10 | return ( 11 |
12 |
13 | 14 |
15 |
加载中...
16 |
17 | ); 18 | }; 19 | -------------------------------------------------------------------------------- /src/layouts/tab-bar/index.less: -------------------------------------------------------------------------------- 1 | .tab-bar-layout { 2 | position: relative; 3 | width: 100%; 4 | height: 100vh; 5 | display: flex; 6 | flex-direction: column; 7 | justify-content: start; 8 | 9 | .container-warp { 10 | position: relative; 11 | flex: 1; 12 | overflow: hidden; 13 | 14 | &:after { 15 | content: ''; 16 | position: absolute; 17 | left: 0; 18 | right: 0; 19 | bottom: 0; 20 | height: 1px; 21 | box-shadow: 0 0 10px 0 #999999; 22 | } 23 | 24 | .view-warp { 25 | position: relative; 26 | overflow-y: auto; 27 | height: 100%; 28 | > div{ 29 | width: 100%; 30 | height: 100%; 31 | } 32 | 33 | &::-webkit-scrollbar { 34 | display: none; 35 | } 36 | 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/pages/login/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Button, ErrorBlock, Toast } from 'antd-mobile'; 3 | import './index.less'; 4 | 5 | const LoginPage = () => { 6 | const [count, setCount] = React.useState(0); 7 | return ( 8 |
9 | {' '} 10 |
11 | 12 |
13 | 我是登录的页面 {count} 14 |
15 | 23 |
24 | 36 |
37 | ); 38 | }; 39 | 40 | export default LoginPage; 41 | -------------------------------------------------------------------------------- /src/theme.less: -------------------------------------------------------------------------------- 1 | // 主题自定义 2 | // https://mobile.ant.design/zh/guide/theming 3 | // 以下是 antd-mobile 目前提供的全局性 CSS 变量; 可以更具自己的需求自行变更 4 | :root { 5 | --adm-font-size-1: 18px; 6 | --adm-font-size-2: 20px; 7 | --adm-font-size-3: 22px; 8 | --adm-font-size-4: 24px; 9 | --adm-font-size-5: 26px; 10 | --adm-font-size-6: 28px; 11 | --adm-font-size-7: 30px; 12 | --adm-font-size-8: 32px; 13 | --adm-font-size-9: 34px; 14 | --adm-font-size-10: 36px; 15 | --adm-color-primary: #1677ff; 16 | --adm-color-success: #00b578; 17 | --adm-color-warning: #ff8f1f; 18 | --adm-color-danger: #ff3141; 19 | --adm-color-white: #fff; 20 | --adm-color-weak: #999; 21 | --adm-color-light: #ccc; 22 | --adm-color-border: #eee; 23 | --adm-font-size-main: var(--adm-font-size-5, 26px); 24 | --adm-color-text: #333; 25 | --adm-font-family: -apple-system, blinkmacsystemfont, 'Helvetica Neue', 26 | helvetica, segoe ui, arial, roboto, 'PingFang SC', 'miui', 27 | 'Hiragino Sans GB', 'Microsoft Yahei', sans-serif; 28 | } 29 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "allowJs": true, 5 | "skipLibCheck": true, 6 | "module": "esnext", 7 | "moduleResolution": "node", 8 | "resolveJsonModule": true, 9 | "importHelpers": true, 10 | "noEmit": true, 11 | "jsx": "react-jsx", 12 | "esModuleInterop": true, 13 | "sourceMap": true, 14 | "baseUrl": "./", 15 | "strict": true, 16 | "paths": { 17 | "@/*": ["src/*"], 18 | "@@/*": ["src/.umi/*"] 19 | }, 20 | "allowSyntheticDefaultImports": true, 21 | "plugins": [ 22 | { 23 | "name": "typescript-styled-plugin" 24 | } 25 | ], 26 | "isolatedModules": true, 27 | "lib": ["dom", "dom.iterable", "esnext"], 28 | "noFallthroughCasesInSwitch": true 29 | }, 30 | "include": ["mock/**/*", "src/**/*", "config/**/*", "typings.d.ts"], 31 | "exclude": [ 32 | "node_modules", 33 | "lib", 34 | "es", 35 | "dist", 36 | "typings", 37 | "**/__test__", 38 | "test", 39 | "docs", 40 | "tests" 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Yanghc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "umi-antd-mobile", 3 | "version": "0.0.1", 4 | "author": "Yanghc", 5 | "description": "基于umi@4.x + antd-mobile@next 快速构建h5及app应用", 6 | "private": true, 7 | "scripts": { 8 | "analyze": "cross-env ANALYZE=1 max build", 9 | "dev": "max dev", 10 | "build": "max build", 11 | "postinstall": "max setup", 12 | "prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'", 13 | "test": "umi-test", 14 | "test:coverage": "umi-test --coverage", 15 | "prepare": "husky" 16 | }, 17 | "gitHooks": { 18 | "pre-commit": "lint-staged" 19 | }, 20 | "engines": { 21 | "node": ">=10.0.0" 22 | }, 23 | "lint-staged": { 24 | "*.{js,jsx,less,md,json}": [ 25 | "prettier --write" 26 | ], 27 | "*.ts?(x)": [ 28 | "prettier --parser=typescript --write" 29 | ] 30 | }, 31 | "dependencies": { 32 | "@umijs/max": "^4.2.11", 33 | "antd-mobile": "^5.36.1", 34 | "antd-mobile-icons": "^0.3.0", 35 | "react": "^18.3.1", 36 | "react-dom": "^18.3.1" 37 | }, 38 | "devDependencies": { 39 | "@types/mockjs": "^1.0.6", 40 | "@types/react": "^18.3.3", 41 | "@types/react-dom": "^18.3.0", 42 | "@umijs/lint": "^4.2.11", 43 | "@umijs/plugin-antd-mobile": "^1.2.0", 44 | "@umijs/test": "^4.0.70", 45 | "eslint": "^8.57.0", 46 | "husky": "^9.0.11", 47 | "lint-staged": "^15.2.7", 48 | "mockjs": "^1.1.0", 49 | "postcss-px-to-viewport": "^1.1.1", 50 | "prettier": "^3.3.2", 51 | "stylelint": "^14", 52 | "typescript": "^5.1.3", 53 | "yorkie": "^2.0.0" 54 | }, 55 | "peerDependencies": {} 56 | } 57 | -------------------------------------------------------------------------------- /src/pages/details/index.tsx: -------------------------------------------------------------------------------- 1 | import { TabBarContext } from '@/layouts'; 2 | import { history } from '@umijs/max'; 3 | import { NavBar, ProgressCircle, Result, Space, Toast } from 'antd-mobile'; 4 | import { useContext } from 'react'; 5 | 6 | export default () => { 7 | const todo = useContext(TabBarContext); 8 | const back = () => { 9 | Toast.show({ 10 | content: '点击了返回区域', 11 | duration: 1000, 12 | afterClose: () => history.back(), 13 | }); 14 | }; 15 | 16 | return ( 17 |
18 | 19 | 详情页面 20 | 21 | 26 | 27 |
28 | 待办数:{todo.items.todoBadge || '0'} 29 |
30 |
31 | 消息数:{todo.items.messageBadge || '0'} 32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
指定线条宽度
40 | 41 | 42 | 75% 43 | 44 | 45 | 75% 46 | 47 | 48 | 75% 49 | 50 | 51 |
52 |
53 | ); 54 | }; 55 | -------------------------------------------------------------------------------- /config/routes.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * 路由配置 3 | * 更多路由请查询 https://umijs.org/zh-CN/docs/routing 4 | */ 5 | export default [ 6 | { path: '/', redirect: '/tab-bar/index' }, 7 | { 8 | path: '/', 9 | layout: '@/layouts/index', // 采用umi 约定的全局路由, 因为umi不能针对不同的路由配置不同的 layout,所以需要在全局的layout中特殊处理。 10 | routes: [ 11 | { 12 | path: '/tab-bar/index', 13 | title: '首页', 14 | icon: 'AlipayCircleFill', 15 | component: '@/pages/home/index', 16 | }, 17 | { 18 | path: '/tab-bar/todo', 19 | title: '我的待办', 20 | badgeKey: 'todoBadge', 21 | icon: 'UnorderedListOutline', 22 | wrappers: [ 23 | // 配置路由的高阶组件封装 24 | '@/wrappers/index', //用于路由级别的权限校验 25 | ], 26 | component: '@/pages/todo/index', 27 | }, 28 | { 29 | path: '/tab-bar/message', 30 | title: '我的消息', 31 | icon: 'MessageOutline', 32 | badgeKey: 'messageBadge', 33 | wrappers: [ 34 | // 配置路由的高阶组件封装 35 | '@/wrappers/index', //用于路由级别的权限校验 36 | ], 37 | component: '@/pages/message/index', 38 | }, 39 | { 40 | path: '/tab-bar/personalCenter', 41 | title: '个人中心', 42 | icon: 'UserOutline', 43 | wrappers: [ 44 | // 配置路由的高阶组件封装 45 | '@/wrappers/index', //用于路由级别的权限校验 46 | ], 47 | component: '@/pages/account/index', 48 | }, 49 | ], 50 | }, 51 | { path: '/detail', title: '详情页', component: '@/pages/details/index' }, 52 | { path: '/login', component: '@/pages/login/index', layout: false }, 53 | { path: '/home', component: '@/pages/home/index', layout: false }, 54 | { path: '/*', component: '@/pages/404', layout: false }, 55 | { path: '/**/*', redirect: '/404', layout: false }, 56 | ]; 57 | -------------------------------------------------------------------------------- /config/config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from '@umijs/max'; 2 | import routes from './routes'; 3 | 4 | /** 5 | * UMI 配置 6 | * 更多相关配置查询 https://umijs.org/zh-CN/docs/config 7 | */ 8 | export default defineConfig({ 9 | // layout: true, 10 | title: 'umi-antd-mobile', 11 | // dynamicImport: { 12 | // loading: "@/pages/loading" 13 | // }, 14 | routes: routes, 15 | // nodeModulesTransform: { 16 | // type: "none" 17 | // }, 18 | layout: false, 19 | alias: { 20 | // "antd-mobile": "antd-mobile/2x" //使用高清适配 21 | }, 22 | // mfsu: { production: { output: ".mfsu-production" } }, 23 | fastRefresh: true, 24 | clickToComponent: {}, 25 | extraBabelPlugins: [], 26 | autoprefixer: { 27 | overrideBrowserslist: [ 28 | 'Android 4.1', 29 | 'iOS 7.1', 30 | 'Chrome > 31', 31 | 'ff > 31', 32 | 'ie >= 8', 33 | 'last 10 versions', // 所有主流浏览器最近10版本用 34 | ], 35 | grid: true, 36 | }, 37 | postcssLoader: {}, 38 | extraPostCSSPlugins: [ 39 | require('postcss-px-to-viewport')({ 40 | viewportWidth: 750, // 视口宽度,对应设计稿的宽度,一般是 375 或 750 41 | viewportHeight: 1334, // 视口高度,根据 750 设备的宽度来指定,一般指定 1334 也可以不配置 42 | unitPrecision: 3, // 指定 `px` 转换为视口单位值的小数位数 43 | viewportUnit: 'vw', // 指定需要转换成的视口单位,建议使用 vw 44 | selectorBlackList: ['.ignore', '.hairlines'], // 指定不转换为视口单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名 45 | minPixelValue: 1, // 小于或等于 `1px` 不转换为视口单位,你也可以设置为你想要的值 46 | mediaQuery: true, // 允许在媒体查询中转换 `px` 47 | }), 48 | ], 49 | metas: [ 50 | { 51 | name: 'keywords', 52 | content: 'umi, umijs, antd-mobile', 53 | }, 54 | { 55 | name: 'description', 56 | content: '基于umi@3.x + antd-mobile@next 快速构建h5及app应用', 57 | }, 58 | { 59 | name: 'viewport', 60 | content: 61 | 'width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no', 62 | }, 63 | ], 64 | // https://mobile.ant.design/zh/guide/quick-start#%E5%85%BC%E5%AE%B9%E6%80%A7 65 | targets: { 66 | chrome: 49, 67 | ios: 10, 68 | }, 69 | }); 70 | -------------------------------------------------------------------------------- /src/layouts/index.tsx: -------------------------------------------------------------------------------- 1 | import { Outlet, useLocation, useOutletContext } from '@umijs/max'; 2 | import { ConfigProvider } from 'antd-mobile'; 3 | import zhCN from 'antd-mobile/es/locales/zh-CN'; 4 | import React, { useMemo, useState } from 'react'; 5 | import routes from '../../config/routes'; 6 | import TabBarLayout from './tab-bar'; 7 | import { BadgeProps } from 'antd-mobile/es/components/badge'; 8 | import './index.less'; 9 | 10 | export interface TabBarItemValueProps { 11 | homeBadge?: BadgeProps['content']; 12 | todoBadge?: BadgeProps['content']; 13 | messageBadge?: BadgeProps['content']; 14 | meBadge?: BadgeProps['content']; 15 | } 16 | 17 | export const TabBarContext = React.createContext<{ 18 | items: TabBarItemValueProps; 19 | callback?: (values: TabBarItemValueProps) => void; 20 | }>({ 21 | items: {}, 22 | }); 23 | 24 | /** 25 | * 不同的全局 layout 26 | * @param props 27 | * @url https://umijs.org/zh-CN/docs/convention-routing#%E4%B8%8D%E5%90%8C%E7%9A%84%E5%85%A8%E5%B1%80-layout 28 | */ 29 | export default () => { 30 | const [taBarItemValues, setTabBarItemValues] = useState< 31 | TabBarItemValueProps | any 32 | >({}); 33 | const { pathname } = useLocation(); 34 | const props = useOutletContext(); 35 | 36 | const getLayoutChildren = useMemo(() => { 37 | if (pathname.startsWith('/tab-bar/')) { 38 | return ( 39 | x.path === '/' && x?.routes)[0]?.routes ?? [] 42 | } 43 | /> 44 | ); 45 | } 46 | 47 | return ( 48 |
49 | 50 |
51 | ); 52 | }, [props]); 53 | 54 | console.log('taBarItemValues', taBarItemValues); 55 | 56 | return ( 57 | 58 | setTabBarItemValues({ ...items }), 62 | }} 63 | > 64 |
{getLayoutChildren}
65 |
66 |
67 | ); 68 | }; 69 | -------------------------------------------------------------------------------- /src/pages/message/index.tsx: -------------------------------------------------------------------------------- 1 | import { Grid, NoticeBar, Space } from 'antd-mobile'; 2 | import { CloseCircleOutline, CompassOutline } from 'antd-mobile-icons'; 3 | import { UmiAntdMobileIcon } from '@/components'; 4 | 5 | export default () => { 6 | return ( 7 | <> 8 | 9 | 10 | 11 | 12 | 13 | 17 | 18 | } 20 | icon={} 21 | content={'自定义图标'} 22 | /> 23 | 26 | 查看详情 27 | 关闭 28 | 29 | } 30 | content={'自定义右侧功能区'} 31 | color="alert" 32 | /> 33 | 34 | 35 |
自定义icon
36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | ); 64 | }; 65 | -------------------------------------------------------------------------------- /src/components/icons/README.md: -------------------------------------------------------------------------------- 1 | # 自定义 font 图标 2 | Icon 提供了一个 createFromIconfontCN 方法,方便开发者调用在 iconfont.cn 上自行管理的图标。 3 | 4 | ```tsx 5 | 6 | import { createFromIconfontCN } from '@/components/icon'; 7 | 8 | const MyIcon = createFromIconfontCN({ 9 | scriptUrl: '//at.alicdn.com/t/font_3162733_un2ldgoxap.js', // 在 iconfont.cn 上生成 10 | }); 11 | 12 | ReactDOM.render(, mountedNode); 13 | 14 | ``` 15 | 16 | 其本质上是创建了一个使用 标签来渲染图标的组件。 17 | 18 | options 的配置项如下: 19 | 20 | | 参数 | 说明 | 类型 | 默认值 | 版本 | 21 | | --- | --- |------------------------|-------| --- | 22 | | extraCommonProps | 给所有的 `svg` 图标 `` 组件设置额外的属性 | { \[key: string]: any } | {} | | 23 | | scriptUrl | [iconfont.cn](http://iconfont.cn/) 项目在线生成的 js 地址,支持 `string[]` 类型 | string \| string\[] | - | | 24 | 25 | 在 `scriptUrl` 都设置有效的情况下,组件在渲染前会自动引入 [iconfont.cn](http://iconfont.cn/) 项目中的图标符号集,无需手动引入。 26 | 27 | 见 [iconfont.cn 使用帮助](http://iconfont.cn/help/detail?spm=a313x.7781069.1998910419.15&helptype=code) 查看如何生成 js 地址。 28 | 29 | ### 注意:目前 `Icon` 导出的是一个空, 没有任何可用图标; 30 | 31 | 32 | ## 项目中使用帮助 33 | 如果需要使用自己的图标,请在 `components` 下的 `index.tx` 文件中修改 `scriptUrl`的链接地址,这个链接地址是在 [iconfont.cn官网](https://www.iconfont.cn) 生成的; 34 | 35 | 然后在项目中导入 `import { UmiAntdMobileIcon } from '@/components';` 即可使用; 36 | 37 | 示例代码: 38 | 39 | ```tsx 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | ``` 67 | 68 | 组件中的 `type` 就是在 `iconfont` 自定义的图标名称 69 | 70 | 更多使用方法请参考 [ant-design-icons](https://ant.design/components/icon-cn/) 71 | 72 | 73 | -------------------------------------------------------------------------------- /src/components/icons/utils/dynamicCSS.ts: -------------------------------------------------------------------------------- 1 | import canUseDom from './canUseDom'; 2 | 3 | const MARK_KEY = `rc-util-key` as any; 4 | 5 | interface Options { 6 | attachTo?: Element; 7 | csp?: { nonce?: string }; 8 | prepend?: boolean; 9 | } 10 | 11 | function getContainer(option: Options) { 12 | if (option.attachTo) { 13 | return option.attachTo; 14 | } 15 | 16 | const head = document.querySelector('head'); 17 | return head || document.body; 18 | } 19 | 20 | export function injectCSS(css: string, option: Options = {}) { 21 | if (!canUseDom()) { 22 | return null; 23 | } 24 | 25 | const styleNode = document.createElement('style'); 26 | if (option.csp?.nonce) { 27 | styleNode.nonce = option.csp?.nonce; 28 | } 29 | styleNode.innerHTML = css; 30 | 31 | const container = getContainer(option); 32 | const { firstChild } = container; 33 | 34 | if (option.prepend && container.prepend) { 35 | // Use `prepend` first 36 | container.prepend(styleNode); 37 | } else if (option.prepend && firstChild) { 38 | // Fallback to `insertBefore` like IE not support `prepend` 39 | container.insertBefore(styleNode, firstChild); 40 | } else { 41 | container.appendChild(styleNode); 42 | } 43 | 44 | return styleNode; 45 | } 46 | 47 | const containerCache: any = new Map(); 48 | 49 | function findExistNode(key: string, option: Options = {}) { 50 | const container = getContainer(option); 51 | 52 | return Array.from(containerCache.get(container).children).find( 53 | (node: any) => node.tagName === 'STYLE' && node[MARK_KEY] === key, 54 | ) as HTMLStyleElement; 55 | } 56 | 57 | export function removeCSS(key: string, option: Options = {}) { 58 | const existNode = findExistNode(key, option); 59 | 60 | existNode?.parentNode?.removeChild(existNode); 61 | } 62 | 63 | export function updateCSS(css: string, key: string, option: Options = {}) { 64 | const container = getContainer(option); 65 | 66 | // Get real parent 67 | if (!containerCache.has(container)) { 68 | const placeholderStyle: any = injectCSS('', option); 69 | const { parentNode } = placeholderStyle; 70 | containerCache.set(container, parentNode); 71 | parentNode.removeChild(placeholderStyle); 72 | } 73 | 74 | const existNode = findExistNode(key, option); 75 | 76 | if (existNode) { 77 | if (option.csp?.nonce && existNode.nonce !== option.csp?.nonce) { 78 | existNode.nonce = option.csp?.nonce; 79 | } 80 | 81 | if (existNode.innerHTML !== css) { 82 | existNode.innerHTML = css; 83 | } 84 | 85 | return existNode; 86 | } 87 | 88 | const newNode: any = injectCSS(css, option); 89 | newNode[MARK_KEY] = key; 90 | return newNode; 91 | } 92 | -------------------------------------------------------------------------------- /src/components/icons/IconFont.tsx: -------------------------------------------------------------------------------- 1 | import * as React from 'react'; 2 | import Icon, { IconBaseProps } from './Icon'; 3 | 4 | const customCache = new Set(); 5 | 6 | export interface CustomIconOptions { 7 | scriptUrl?: string | string[]; 8 | extraCommonProps?: { [key: string]: any }; 9 | } 10 | 11 | export interface IconFontProps 12 | extends IconBaseProps { 13 | type: T; 14 | } 15 | 16 | function isValidCustomScriptUrl(scriptUrl: string): boolean { 17 | return Boolean( 18 | typeof scriptUrl === 'string' && 19 | scriptUrl.length && 20 | !customCache.has(scriptUrl), 21 | ); 22 | } 23 | 24 | function createScriptUrlElements( 25 | scriptUrls: string[], 26 | index: number = 0, 27 | ): void { 28 | const currentScriptUrl = scriptUrls[index]; 29 | if (isValidCustomScriptUrl(currentScriptUrl)) { 30 | const script = document.createElement('script'); 31 | script.setAttribute('src', currentScriptUrl); 32 | script.setAttribute('data-namespace', currentScriptUrl); 33 | if (scriptUrls.length > index + 1) { 34 | script.onload = () => { 35 | createScriptUrlElements(scriptUrls, index + 1); 36 | }; 37 | script.onerror = () => { 38 | createScriptUrlElements(scriptUrls, index + 1); 39 | }; 40 | } 41 | customCache.add(currentScriptUrl); 42 | document.body.appendChild(script); 43 | } 44 | } 45 | 46 | export default function create( 47 | options: CustomIconOptions = {}, 48 | ): React.FC> { 49 | const { scriptUrl, extraCommonProps = {} } = options; 50 | 51 | /** 52 | * DOM API required. 53 | * Make sure in browser environment. 54 | * The Custom Icon will create a