Skip to content

Commit

Permalink
optimization: style, utils and more
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmero committed May 8, 2024
1 parent 494024d commit c58fbcb
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 45 deletions.
7 changes: 1 addition & 6 deletions docs/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,14 @@ body {
margin-bottom: 1em;
}

.alert-html {
width: 100%;
max-width: 550px;
}

.gm-label {
display: flex;
align-items: center;
justify-content: center;
}

.gm-input {
width: calc(100% - 85px);
width: calc(100% - 6em);
padding: 10px 12px;
font-size: 14px;
color: var(--text);
Expand Down
2 changes: 1 addition & 1 deletion docs/index.js

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gmalert",
"version": "1.5.2",
"version": "1.6.0",
"description": "A simple but powerful alert component",
"types": "dist/types/main.d.ts",
"module": "dist/gmalert.esm.js",
Expand Down Expand Up @@ -34,29 +34,29 @@
"author": "歌梦罗",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.23.5",
"@babel/preset-env": "^7.23.5",
"@babel/preset-typescript": "^7.23.3",
"@babel/core": "^7.24.5",
"@babel/preset-env": "^7.24.5",
"@babel/preset-typescript": "^7.24.1",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"@xbmlz/eslint-config-prettier": "^0.1.17",
"@xbmlz/eslint-config-ts": "^0.1.17",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"eslint": "^8.55.0",
"eslint": "^8.57.0",
"npm-run-all": "^4.1.5",
"postcss-preset-env": "^9.3.0",
"postcss-preset-env": "^9.5.11",
"rollup": "^3.29.4",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-serve": "^2.0.3",
"sass": "^1.69.5",
"sass": "^1.77.0",
"stylelint": "^15.11.0",
"stylelint-config-recess-order": "^4.4.0",
"stylelint-config-recess-order": "^4.6.0",
"stylelint-config-standard-scss": "^11.1.0",
"tslib": "^2.6.2",
"typescript": "^5.3.2"
"typescript": "^5.4.5"
}
}
7 changes: 1 addition & 6 deletions src/demo/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,14 @@ body {
margin-bottom: 1em;
}

.alert-html {
width: 100%;
max-width: 550px;
}

.gm-label {
display: flex;
align-items: center;
justify-content: center;
}

.gm-input {
width: calc(100% - 85px);
width: calc(100% - 6em);
padding: 10px 12px;
font-size: 14px;
color: var(--text);
Expand Down
4 changes: 2 additions & 2 deletions src/demo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { alert, message, notice } from '../main'
import { newDiv } from '../utils/html'
import { newDiv, querySelector } from '../utils/html'
import InformationBtnBox from './information'

import './index.scss'
Expand All @@ -14,7 +14,7 @@ import '../styles/message.scss'

notice.config({ timeout: 5000 })

const $root = document.querySelector('#root')
const $root = querySelector('#root')

if (!$root) {
throw new Error('Root element not found')
Expand Down
4 changes: 2 additions & 2 deletions src/demo/information.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { information } from '../main'
import { newDiv } from '../utils/html'
import { newDiv, newEl } from '../utils/html'

function Button(text: string, callback: () => void) {
const button = document.createElement('button')
const button = newEl('button')
button.classList.add('btn')
button.textContent = text
button.onclick = callback
Expand Down
6 changes: 4 additions & 2 deletions src/modules/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
cn,
getContainer,
newDiv,
newEl,
querySelector,
} from '../utils/html'
import { AnimatedIcon } from '../component/animatedIcons/animatedIcons'
import { MakeMsg } from '../core/Msg'
Expand All @@ -13,7 +15,7 @@ import type { MsgType } from './message'
import type { PropsAlert } from './types'

function Button(text: string, onClick: () => void) {
const $btn = document.createElement('button')
const $btn = newEl('button')
$btn.textContent = text
$btn.onclick = onClick
$btn.classList.add(cn('alert-btn'))
Expand Down Expand Up @@ -70,7 +72,7 @@ export function GmAlert(props: PropsAlert): MsgType {
animationendHandle($wrapper, (e: string) => {
if (e === cn('alert-out')) {
$box.remove()
if (!document.querySelector(`.${cn('alert')}`)) {
if (!querySelector(`.${cn('alert')}`)) {
bodyScroll(false)
}
props.onClosed(status)
Expand Down
4 changes: 2 additions & 2 deletions src/modules/information.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { animationendHandle, changeAnimation } from '../utils/animateHandle'
import { changeStyle, cn, getContainer, newDiv } from '../utils/html'
import { changeStyle, cn, getContainer, newDiv, querySelector } from '../utils/html'
import { MakeMsg } from '../core/Msg'
import { CloseIcon } from '../component/icons/close'
import type { MsgType } from './message'
Expand Down Expand Up @@ -44,7 +44,7 @@ export function GmInformation(props: PropsInfo): MsgType {
}

if (!props.hideClose) {
const $head = $wrapper.querySelector<HTMLElement>(`.${cn('info-header')}`)!
const $head = querySelector<HTMLElement>(`.${cn('info-header')}`, $wrapper)!
const $close = CloseIcon()
$close.onclick = () => {
close(0)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function GmMessage(props: PropsMessage): MsgType {

const close = (status: number) => {
props.onClose()
$main.style.maxHeight = `${$main.offsetHeight}px`
changeStyle($main, [`max-height: ${$main.offsetHeight}px`])
changeAnimation($wrapper, cn('msg-out'))
changeAnimation($main, cn('msg-close'))

Expand Down
6 changes: 3 additions & 3 deletions src/modules/notice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { animationendHandle, changeAnimation } from '../utils/animateHandle'
import { changeStyle, cn, getNoticeContainer, newDiv } from '../utils/html'
import { changeStyle, cn, getNoticeContainer, newDiv, querySelector } from '../utils/html'
import { AnimatedIcon } from '../component/animatedIcons/animatedIcons'
import { MakeMsg } from '../core/Msg'
import type { MsgType } from './message'
Expand All @@ -18,9 +18,9 @@ export function GmNotice(props: PropsMessage): MsgType {
changeStyle($wrapper, [`--mh:${$wrapper.offsetHeight + 10}px`])
changeAnimation($wrapper, cn('open'))
setTimeout(() => {
const $icon = $wrapper.querySelector<HTMLElement>(`.${cn('notice-icon')}`)
const $icon = querySelector<HTMLElement>(`.${cn('notice-icon')}`,$wrapper)
if ($icon) {
$icon.style.opacity = '1'
changeStyle($icon, ['opacity:1'])
}
}, 300)
}
Expand Down
33 changes: 23 additions & 10 deletions src/utils/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export function newDiv(...className: string[]) {

export function setMsgCount($el: HTMLElement, count: number) {
const countClassName = cn('count')
let $count = $el.querySelector(`.${countClassName}`) as HTMLElement
let $count = querySelector(`.${countClassName}`, $el) as HTMLElement
if (!$count) {
$count = document.createElement('span')
$count = newEl('span')
$count.classList.add(countClassName)

$el.append($count)
Expand All @@ -35,7 +35,7 @@ export function setMsgCount($el: HTMLElement, count: number) {
}

export const getContainer = () => {
let $root = document.querySelector<HTMLElement>(`.gmal`)
let $root = querySelector<HTMLElement>(`.gmal`)

if (!$root) {
$root = newDiv('gmal')
Expand All @@ -46,15 +46,15 @@ export const getContainer = () => {
}

export const getMessageContainer = () => {
let $root = document.querySelector<HTMLElement>(`.${cn('msg-box')}`)
let $root = querySelector<HTMLElement>(`.${cn('msg-box')}`)
if ($root) return $root
$root = newDiv(cn('msg-box'))
getContainer().append($root)
return $root
}

export const getNoticeContainer = () => {
let $wrapper = document.querySelector<HTMLElement>(`.${cn('notice-box')}`)
let $wrapper = querySelector<HTMLElement>(`.${cn('notice-box')}`)
if ($wrapper) return $wrapper
$wrapper = newDiv(cn('notice-box'))
getContainer().append($wrapper)
Expand All @@ -70,17 +70,30 @@ export function changeStyle(el: HTMLElement, arr: string[]): void {
el.style.cssText = `${original.concat(arr).join(';')};`
}

// 用于重置样式
export function resetStyle(el: HTMLElement, arr: string[]): void {
arr.forEach((item) => {
el.style.removeProperty(item)
})
}

// 用于设置滚动条
export function bodyScroll(lock = true) {
const $body = document.body
if (lock) {
// set padding
$body.style.paddingRight = `${
changeStyle($body, ['overflow: hidden', `padding-right: ${
window.innerWidth - document.documentElement.clientWidth
}px`
$body.style.overflow = 'hidden'
}px`])
} else {
$body.style.overflow = ''
$body.style.paddingRight = ''
resetStyle($body, ['overflow', 'padding-right'])
}
}

// 用于获取元素
export function querySelector<T extends HTMLElement = HTMLElement>(
selector: string,
$el: HTMLElement = document.body,
): T {
return $el.querySelector(selector) as T
}

0 comments on commit c58fbcb

Please sign in to comment.