Skip to content

Commit

Permalink
feat: add new component **GlobalStatus** to the lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Aug 19, 2019
1 parent 5b8cb19 commit 89ca72f
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const InputWithError = () => {
on_change={({ value }) => {
setErrorMessage(value.length >= 3 ? 'With a message shown' : null)
}}
status_id="main-status"
global_status_id="main-status"
/>
)
}
Expand Down Expand Up @@ -114,12 +114,12 @@ render(
const [count, toggleUpdateStatus] = useState(0)
return (
<>
<GlobalStatus id="custom-status" />
<GlobalStatus id="custom-status" autoscroll={false} />
<Button
text={'Show step #' + count}
on_click={() => {
toggleUpdateStatus(count + 1)
if (count > 1) {
if (count >= 3) {
toggleUpdateStatus(0)
}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
draft: true
---

| Properties | Description |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | _(optional)_ the main ID. Defaults to the prop |
| `title` | _(optional)_ the title appears as the status content. Defaults to the prop `default_title`. |
| `default_title` | _(optional)_ the default title. Defaults to `En feil har skjedd`. |
| `text, children` | _(optional)_ the text appears as the status content. Beside plain text, You can send in a React component as well. |
| `items` | _(optional)_ the items (list items) appears as a part of the status content. You can both use an JSON array, or a vanilla array with a string `['Item #1', Item #2]` or an object content `[{text: 'Item #1', status_id=1}, {text: 'Item #2', status_id=2}]`. |
| `state` | _(optional)_ defines the visual appearance of the status. There are two main states `error` and `info`. The default status is `error`. |
| `icon` | _(optional)_ the icon show before the status title. Defaults to `exclamation`. |
| `icon_size` | _(optional)_ the icon size of the title icon shows. Defaults to `medium`. |
| `show` | _(optional)_ set to `true` to manually make the global status visible. Defaults to `false`. |
| `autoclose` | _(optional)_ set to `true` to automatically close the global status if there are no more left items in the provider stack. Defaults to `true`. |
| `autoscroll` | _(optional)_ set to `true` to automatically scroll the page to the appeared global status. Defaults to `true`. |
| `no_animation` | _(optional)_ set to `true` to disable the show/hide/slide/fade/grow/shrink animation. Defaults to `false`. |
| `delay` | _(optional)_ defines the delay on how long the automated visibility should wait, before it appears to the user. Defaults to `100ms`. |
| `hide_close_button` | _(optional)_ set to `true` if the close button should be hidden for the user. Defaults to `false`. |
| `close_text` | _(optional)_ text of the close button. Defaults to `Lukk`. |
| `status_anchor_texts` | _(optional)_ defines the anchor text showing up after every item, in case there is a `status_id` defined. Defaults to `Gå til`. |
| Properties | Description |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | _(optional)_ the main ID. Defaults to the prop |
| `title` | _(optional)_ the title appears as the status content. Defaults to the prop `default_title`. |
| `default_title` | _(optional)_ the default title. Defaults to `En feil har skjedd`. |
| `text, children` | _(optional)_ the text appears as the status content. Beside plain text, You can send in a React component as well. |
| `items` | _(optional)_ the items (list items) appears as a part of the status content. You can both use an JSON array, or a vanilla array with a string `['Item #1', Item #2]` or an object content `[{text: 'Item #1', status_id=1, status_anchor_url=true}, {text: 'Item #2', status_id=2, status_anchor_url=true}]`. Use `status_anchor_url="true"` to enable the go to link, or provide it with an actual url: `status_anchor_url="https://"` |
| `state` | _(optional)_ defines the visual appearance of the status. There are two main states `error` and `info`. The default status is `error`. |
| `icon` | _(optional)_ the icon show before the status title. Defaults to `exclamation`. |
| `icon_size` | _(optional)_ the icon size of the title icon shows. Defaults to `medium`. |
| `show` | _(optional)_ set to `true` to manually make the global status visible. Defaults to `false`. |
| `autoclose` | _(optional)_ set to `true` to automatically close the global status if there are no more left items in the provider stack. Defaults to `true`. |
| `autoscroll` | _(optional)_ set to `true` to automatically scroll the page to the appeared global status. Defaults to `true`. |
| `no_animation` | _(optional)_ set to `true` to disable the show/hide/slide/fade/grow/shrink animation. Defaults to `false`. |
| `delay` | _(optional)_ defines the delay on how long the automated visibility should wait, before it appears to the user. Defaults to `200ms`. |
| `hide_close_button` | _(optional)_ set to `true` if the close button should be hidden for the user. Defaults to `false`. |
| `close_text` | _(optional)_ text of the close button. Defaults to `Lukk`. |
| `status_anchor_texts` | _(optional)_ defines the anchor text showing up after every item, in case there is a `status_id` defined. Defaults to `Gå til`. |

## Controllers

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Animation helper
*
* NB: For now only used in GlobalStatus,
* but can be of interest to use in other components as well
*
*/

export default class Animation {
stack = []
events = []

// Main methodes
add(animation) {
if (!animation.type) {
console.warn('You should define an animation type.')
}
if (this.isInProgress() && animation.type === this.stack[0].type) {
return
}
this.stack.push(animation)

this.runNext()
}
unbind() {
clearTimeout(this._durationId)
clearTimeout(this._delayId)
this.stack = []
this.events = []
}

// Helpers
getCurrent() {
return this.isInProgress() ? this.stack[0] : null
}
isEmpty() {
return this.stack.length === 0
}
isInProgress() {
return this.stack.length > 0
}

// Internals
runNext() {
if (this.stack.length > 1) {
return
}
const animation = this.stack[0]

if (!animation) {
return
}
const run = () => {
if (typeof animation.onStart === 'function') {
animation.onStart(animation)
}
this.runGlobalEvents(animation, 'onStart')
const next = () => {
// now, remove the one we have processed
this.stack = this.stack.filter(a => a !== animation)
if (typeof animation.onComplete === 'function') {
animation.onComplete(animation)
}
this.runGlobalEvents(animation, 'onComplete')
this.runNext()
}
if (animation.duration > 0) {
this._durationId = setTimeout(next, animation.duration)
} else {
next()
}
}
if (animation.delay > 0) {
this._delayId = setTimeout(run, animation.delay)
} else {
run()
}

return animation
}

// Global events
onStart(callback) {
this.events.push({ callback, type: 'onStart' })
}
onComplete(callback) {
this.events.push({ callback, type: 'onComplete' })
}
runGlobalEvents(animation, type) {
this.events.forEach(event => {
if (type === event.type && typeof event.callback === 'function') {
event.callback(animation)
}
})
this.events = []
}
}
Loading

0 comments on commit 89ca72f

Please sign in to comment.