Skip to content

Commit

Permalink
Refactor Toast using Notice wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
c0bra committed Jun 6, 2019
1 parent ba8165d commit 953fda4
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 148 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<a href="https://www.npmjs.com/package/buefy"><img src="https://img.shields.io/npm/v/svelma.svg" /></a>
<a href="https://www.npmjs.com/package/svelma"><img src="https://img.shields.io/npm/l/svelma.svg" /></a>
<a href="https://bundlephobia.com/result?p=svelma"><img src="https://badgen.net/bundlephobia/minzip/svelma"></a>
<!-- <a href="https://circleci.com/gh/c0bra/svelma"><img src="https://img.shields.io/circleci/project/c0bra/svelma/svelma.svg?style=flat-square" /></a> -->
<!-- <a href="https://codecov.io/gh/svelma/svelma"><img src="https://img.shields.io/codecov/c/github/svelma/svelma.svg?style=flat-square" /></a> -->

Expand Down
19 changes: 10 additions & 9 deletions docs/src/routes/components/jsdocs.json
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,12 @@
"Message": [],
"Modal": [],
"Notice": [],
"Notices": [],
"Toast": [
{
"meta": {
"filename": "Toast.svelte",
"lineno": 9,
"lineno": 7,
"columnno": 2,
"path": "src/components/Toast"
},
Expand All @@ -555,17 +556,18 @@
{
"meta": {
"filename": "Toast.svelte",
"lineno": 16,
"lineno": 12,
"columnno": 2,
"path": "src/components/Toast"
},
"description": "Duration toast will remain on screen",
"name": "duration",
"kind": "prop",
"type": [
"Number"
],
"optional": true,
"defaultvalue": 2000,
"name": "duration",
"_isSvelteDoc": true,
"_svelteProps": {
"type": {
Expand All @@ -577,7 +579,6 @@
"defaultvalue": 2000,
"name": "duration"
},
"kind": "prop",
"longname": "module:Toast~duration",
"scope": "inner",
"memberof": "module:Toast",
Expand All @@ -586,17 +587,18 @@
{
"meta": {
"filename": "Toast.svelte",
"lineno": 21,
"lineno": 18,
"columnno": 2,
"path": "src/components/Toast"
},
"description": "Where the toast will show on the screen",
"name": "position",
"kind": "prop",
"type": [
"String"
],
"optional": true,
"defaultvalue": "is-top",
"name": "position",
"_isSvelteDoc": true,
"_svelteProps": {
"type": {
Expand All @@ -608,7 +610,6 @@
"defaultvalue": "is-top",
"name": "position"
},
"kind": "prop",
"values": "<code>is-top</code>, <code>is-bottom</code>, <code>is-top-left</code>, <code>is-top-right</code>, <code>is-bottom-left</code>, <code>is-bottom-right</code>",
"longname": "module:Toast~position",
"scope": "inner",
Expand All @@ -618,7 +619,7 @@
{
"meta": {
"filename": "Toast.svelte",
"lineno": 27,
"lineno": 25,
"columnno": 2,
"path": "src/components/Toast"
},
Expand Down Expand Up @@ -650,7 +651,7 @@
{
"meta": {
"filename": "Toast.svelte",
"lineno": 33,
"lineno": 31,
"columnno": 2,
"path": "src/components/Toast"
},
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"prettier-plugin-svelte": "^0.5.1",
"rollup": "^1.2.2",
"rollup-plugin-alias": "^1.5.1",
"rollup-plugin-analyzer": "^3.0.1",
"rollup-plugin-bundle-size": "^1.0.3",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-copy": "^2.0.1",
Expand All @@ -56,5 +57,5 @@
"autodocs": "(cd docs; npx sapper dev --basepath svelma)",
"jsdocs": "(cd docs; npx jsdoc -c jsdoc/conf.js)"
},
"pre-publish": "build"
"prepublish": "build"
}
4 changes: 3 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import analyze from 'rollup-plugin-analyzer'
import bundleSize from 'rollup-plugin-bundle-size'
import commonjs from 'rollup-plugin-commonjs'
import resolve from 'rollup-plugin-node-resolve'
Expand Down Expand Up @@ -47,7 +48,8 @@ export default {

production && terser(),

bundleSize(),
production && analyze(),
production && bundleSize(),
],
watch: {
clearScreen: false,
Expand Down
135 changes: 89 additions & 46 deletions src/components/Notice.svelte
Original file line number Diff line number Diff line change
@@ -1,61 +1,104 @@
<script context="module">
// import { writable } from 'svelte/store'

// export let noticesTop
// export let noticesBottom
export const notices = {}
export function fitlerProps(props) {
const { active, type, position, duration } = props
return { active, type, position, duration }
}
</script>

<script>
// import { onMount } from 'svelte'

// const div = () => document.createElement('div')

// onMount(() => {
// if (!noticesTop) {
// noticesTop = div()
// noticesTop.className = 'notices is-top'
// document.body.appendChild(noticesTop)
// }
// if (!noticesBottom) {
// noticesBottom = div()
// noticesBottom.className = 'notices is-bottom'
// document.body.appendChild(noticesBottom)
// }
// })
import { createEventDispatcher, onDestroy, onMount } from 'svelte'
import { fly, fade } from 'svelte/transition'
import Notices, { notices } from './Notices.svelte'

const dispatch = createEventDispatcher()

export let active = true
export let type = 'is-dark'
export let position = 'is-top'
export let duration = 2000

let el
let parent
let timer
const div = () => document.createElement('div')

$: transitionY = ~position.indexOf('is-top') ? -200 : 200

function close() {
active = false
}

function remove() {
clearTimeout(timer)

// Just making sure
active = false

dispatch('destroyed')
}

function setupContainers() {
if (!notices.top) {
notices.top = div()
notices.top.className = 'notices is-top'
document.body.appendChild(notices.top)
}
if (!notices.bottom) {
notices.bottom = div()
notices.bottom.className = 'notices is-bottom'
document.body.appendChild(notices.bottom)
}
}

function chooseParent() {
parent = notices.top
if (position && position.indexOf('is-bottom') === 0) parent = notices.bottom

// el.parentNode.removeChild(el)
parent.insertAdjacentElement('afterbegin', el)
}

onMount(() => {
setupContainers()
chooseParent()

timer = setTimeout(() => {
close()
}, duration)
})
</script>

<style lang="sass">
:global(.notices) {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
padding: 3em;
z-index: 1000;
pointer-events: none;
display: flex;

&.is-top {
flex-direction: column;
}
.notice {
display: inline-flex;
pointer-events: auto;

&.is-top,
&.is-bottom {
flex-direction: column-reverse;
align-self: center;
}

[class*='has-background-'] * {
color: transparent !important;
filter: invert(1) brightness(2.5) grayscale(1) contrast(9);
background: inherit;
background-clip: text !important;
-webkit-background-clip: text !important;
&.is-top-left,
&.is-bottom-left {
align-self: flex-start;
}

&.is-top-right,
&.is-bottom-right {
align-self: flex-end;
}
}
</style>

<div class="notice">
<slot />
</div>
{#if active}
<div
class="notice {position}"
aria-hidden={!active}
in:fly={{ y: transitionY }}
out:fade
on:outroend={remove}
bind:this={el}>

<slot />
</div>
{/if}
36 changes: 36 additions & 0 deletions src/components/Notices.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script context="module">
export const notices = {}
</script>

<style lang="sass">
:global(.notices) {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: hidden;
padding: 3em;
z-index: 1000;
pointer-events: none;
display: flex;

&.is-top {
flex-direction: column;
}

&.is-bottom {
flex-direction: column-reverse;
}

[class*='has-background-'] * {
color: transparent !important;
filter: invert(1) brightness(2.5) grayscale(1) contrast(9);
background: inherit;
background-clip: text !important;
-webkit-background-clip: text !important;
}
}
</style>

<div class="notice" />
Loading

0 comments on commit 953fda4

Please sign in to comment.