Skip to content

Commit

Permalink
[gem-book] Improve mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Dec 26, 2023
1 parent 890af84 commit 369a08f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/duoyun-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "duoyun-ui",
"version": "1.1.6",
"version": "1.1.7",
"description": "A lightweight desktop UI component library, implemented using Gem",
"keywords": [
"frontend",
Expand Down Expand Up @@ -48,7 +48,7 @@
"elkjs": "^0.7.1"
},
"peerDependencies": {
"@mantou/gem": "^1.7.2"
"@mantou/gem": "^1.7.6"
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/gem-book/src/element/elements/icons.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { raw } from '@mantou/gem';

export const icons = {
menu: raw`
<svg part="icon" aria-hidden="true" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor">
<path d="M0 0h24v24H0z" fill="none" stroke="none"></path>
<path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"></path>
</svg>
`,
close: raw`
<svg part="icon" aria-hidden="true" height="24px" viewBox="0 0 24 24" width="24px" fill="currentColor">
<path d="M0 0h24v24H0z" fill="none" stroke="none"></path>
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path>
</svg>
`,
i18n: raw`
<svg aria-hidden="true" width="24" height="24" viewBox="0 0 24 24">
<path d="M0 0h24v24H0z" fill="none"></path>
Expand Down
11 changes: 11 additions & 0 deletions packages/gem-book/src/element/elements/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { capitalize, isSameOrigin } from '../lib/utils';
import { bookStore } from '../store';

import { icons } from './icons';
import { sidebarStore, toggleSidebar } from './sidebar';

import '@mantou/gem/elements/link';
import '@mantou/gem/elements/use';
Expand All @@ -28,6 +29,7 @@ import './nav-logo';
*/
@customElement('gem-book-nav')
@connectStore(bookStore)
@connectStore(sidebarStore)
export class Nav extends GemElement {
@boolattribute logo: boolean;

Expand Down Expand Up @@ -148,7 +150,15 @@ export class Nav extends GemElement {
gem-use {
width: 1em;
}
.menu {
display: none;
width: 2.25rem;
margin-inline-end: 1rem;
}
@media ${mediaQuery.PHONE} {
.menu {
display: block;
}
gem-book-nav-logo + .item {
margin-left: 0;
}
Expand All @@ -160,6 +170,7 @@ export class Nav extends GemElement {
}
}
</style>
<gem-use class="menu" @click=${toggleSidebar} .element=${sidebarStore.open ? icons.close : icons.menu}></gem-use>
<div class="internals">
${this.logo ? html`<gem-book-nav-logo></gem-book-nav-logo>` : ''} ${internals.map(this.renderInternalItem)}
</div>
Expand Down
46 changes: 39 additions & 7 deletions packages/gem-book/src/element/elements/sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { html, GemElement, customElement, TemplateResult, connectStore, classMap } from '@mantou/gem';
import {
html,
GemElement,
customElement,
TemplateResult,
connectStore,
classMap,
state,
createStore,
connect,
updateStore,
history,
disconnect,
} from '@mantou/gem';
import { mediaQuery } from '@mantou/gem/helper/mediaquery';

import { NavItem } from '../../common/config';
Expand All @@ -13,9 +26,17 @@ import '@mantou/gem/elements/use';
import './side-link';
import './nav-logo';

export const sidebarStore = createStore({ open: false });

const closeSidebar = () => updateStore(sidebarStore, { open: false });
export const toggleSidebar = () => updateStore(sidebarStore, { open: !sidebarStore.open });

@customElement('gem-book-sidebar')
@connectStore(bookStore)
@connectStore(sidebarStore)
export class SideBar extends GemElement {
@state open: boolean;

#toggleLinks = (e: MouseEvent) => {
const ele = e.target as HTMLDivElement;
ele.classList.toggle('close');
Expand Down Expand Up @@ -72,6 +93,7 @@ export class SideBar extends GemElement {
};

render() {
this.open = sidebarStore.open;
return html`
<style>
:host {
Expand Down Expand Up @@ -102,13 +124,18 @@ export class SideBar extends GemElement {
}
@media ${mediaQuery.PHONE} {
:host {
position: static;
height: auto;
margin: 0;
padding: 0;
overflow: visible;
position: fixed;
background: ${theme.backgroundColor};
width: 100%;
height: calc(100vh - ${theme.headerHeight});
top: ${theme.headerHeight};
z-index: 3;
}
:host(:not(:where([data-open], :state(open)))) {
display: none;
}
.nav {
border-inline-end: none;
border-block-end: 1px solid ${theme.borderColor};
}
gem-book-nav-logo {
display: none;
Expand Down Expand Up @@ -208,4 +235,9 @@ export class SideBar extends GemElement {
};
removeCloseClass(activeEle);
}

mounted() {
connect(history.store, closeSidebar);
return () => disconnect(history.store, closeSidebar);
}
}
5 changes: 0 additions & 5 deletions packages/gem-book/src/element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,6 @@ export class GemBookElement extends GemElement {
main {
padding-inline: 1rem;
}
gem-book-nav ~ gem-book-sidebar {
margin-top: 0;
height: auto;
max-height: none;
}
slot[name='${this.mainBefore}'] {
margin-top: 1rem;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gem-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"@mantou/gem": "^1.7.6",
"duoyun-ui": "^1.1.6"
"duoyun-ui": "^1.1.7"
},
"devDependencies": {
"@gemjs/config": "^1.6.11",
Expand Down

0 comments on commit 369a08f

Please sign in to comment.