Skip to content

Commit

Permalink
Merge pull request #11 from ReeceM/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ReeceM authored Jun 7, 2020
2 parents e955baa + bdacefc commit e19fe36
Show file tree
Hide file tree
Showing 17 changed files with 1,297 additions and 492 deletions.
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
src
docs
postcss.config.js
tailwind.config.js
.editorconfig
.github
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,21 @@ When you set this and the banner is dismissed, the UTC milliseconds are stored i
If you fail to set the value properly, it won't dismiss and the banner will show by default.


### Testing
## Testing

_to come_ please make a PR if you know how to do it on JS.

### Changelog
## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security
## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.
If you discover any security related issues, please email [email protected] instead of using the issue tracker. You can also use the [SECURITY](SECURITY.md) doc.

## Credits

Expand Down
808 changes: 528 additions & 280 deletions dist/hBar.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion dist/hBar.min.js

Large diffs are not rendered by default.

Empty file added docs/.nojekyll
Empty file.
229 changes: 229 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
# h-bar lightweight announcement bar

An Announcement Banner that is lightweight and customizable

[![Latest Version on npm](https://img.shields.io/npm/v/@reecem/h-bar.svg?style=flat-square)](https://www.npmjs.com/package/@reecem/h-bar)
[![Total Downloads](https://img.shields.io/npm/dt/@reecem/h-bar.svg?style=flat-square)](https://www.npmjs.com/package/@reecem/h-bar)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/@reecem/h-bar)
[![](https://data.jsdelivr.com/v1/package/npm/@reecem/h-bar/badge)](https://www.jsdelivr.com/package/npm/@reecem/h-bar)

<h1 align="center">
<br>
<img src="../h-bar.png" alt="h-bar announcements">
<br>
<br>
h-bar Announcement banner
<br>
</h1>

The announcement bar uses native methods to make the library lightweight so that it can be loaded quickly, bar the webpack stuff.

**Note**
The initial version makes use of the WordPress API to be able to get the latest post.

i.e. it expects a json structure like so:
From a url like `http://blog.example.com/wp-json/wp/v2/posts?per_page=1&_fields=id,title,link`
```json
[
{
"id": 175,
"link": "https:\/\/blog.example.com\/how-to-hunt-a-vole\/",
"title": {
"rendered": "How To Hunt A Vole"
}
}
]
```

I plan to add more options and a parser callback that can be defined to extract a standard format.

## Installation

You can install the package via npm:

```bash
npm i @reecem/h-bar
```

Or use jsDelivr:
```html
...
<script src"https://cdn.jsdelivr.net/npm/@reecem/h-bar@latest/dist/hBar.min.js"></script>
...
```

> If you are customising the styling and overriding it with your own styling then you will also need an instance of your css or a tailwindcss file installed as only the classes needed are packaged with h-bar
## Example page

You can view an [example page](https://reecem.github.io/h-bar/example.html)

## Usage

You can import it directly into your javascript app or use it in the html.

```html
<script src="../hBar.js"></script>
<!-- or -->
<script src"https://cdn.jsdelivr.net/npm/@reecem/h-bar@latest/dist/hBar.min.js" defer></script>
<script>
hBar.init({
url: "http://blog.example.com/wp-json/wp/v2/posts?per_page=1&_fields=id,excerpt,title,link",
secondaryLinks: [
{
title: "Docs",
link: "http:://docs.example.com"
},
{
title: "Support",
link: "http://help.example.com"
}
],
options: {
theme: "blue",
}
});
hBar.fetchData()
</script>
```

The initialization object currently has this structure and defaults:

```javascript
{
url: "https://your.blog/api/....",
onCompleted: "callback function",
link: "The link url, can be force and no need to fetch from API",
title: "The link url, can be force and no need to fetch from API",
secondaryLinks: [
{
title: "Docs",
link: "http:://docs.example.com"
}
],
parser: (data) => {/** Parser function */}
dismissible: false, // dismissible banner flag
dismissFor: new Date('2020-03-30'), // would dismiss it till end of March 30th 2020
theme: "gray",
headers: {
"Authorization": "Bearer {TOKEN}"
},
customStyles: {
wrapper: "hb-flex hb-w-full hd-flex-col md:hb-flex-row sm:hb-flex-row hb-text-sm hb-py-2 md:hb-px-20 hb-px-1 hb-items-center hb-justify-between",
linkWrapper: "hb-flex hb-items-center",
badge: "hb-px-2 hb-mx-2 hb-leading-relaxed hb-tracking-wider hb-uppercase hb-font-semibold hb-rounded-full hb-text-xs",
postTitle: "hover:hb-underline",
secondaryLink: "hb-mx-5 hb-cursor-pointer hover:hb-underline",
}
}
```

### Parser function

There is the availability of adding a custom parser function to override any of the default ones provided by the package.

This is handy if you have a custom endpoint that say would return also the secondary links or has a different data structure.

The parser function should always return an object with the structure:
```javascript
{
title: String,
link: String,
/** the secondaryLinks is optional.
* It will also override the links parsed in the init() arguments.
*/
secondaryLinks: [
{
title: String,
link: String,
},
]
}
```

You can define the function inside the `init()` method as follows:
```javascript

hBar.init({
url: "https://api.github.com/repos/ReeceM/h-bar/releases",
parser: (data) => {
// getting the first release on the list of releases from github.
const {name, html_url} = data[0];

return {
title: `Lateset version available ${name}`,
link: html_url
};
}
})
```

### Dismissing Notifications

> Available from `v0.3.0`/`v1.0.0`
**Temporary Dismissing**
To be able to dismiss a notification, please note it currently removes secondary links. It is therefore useful that you use this feature when just making announcements of a event or brief notification.

The way to activate session based dismissal is:

```javascript
{
//... rest of config
dismissible: true,
//... rest of config
}
```

This will just disable the banner for the current page visit, if the user reloads, its back.

**Time based dismissing**

To dismiss the banner until another time, you can set the `dismissFor` variable, this requires a `Date()` object.

When you set this and the banner is dismissed, the UTC milliseconds are stored in the localStorage, this is then read back when loading h-Bar.

```javascript
{
//... rest of config
dismissible: true,
dismissFor: new Date('2020-03-30'), // would dismiss it till end of march 30th
}
```

If you fail to set the value properly, it won't dismiss and the banner will show by default.


## Testing

_to come_ please make a PR if you know how to do it on JS.

## Changelog

Please see [CHANGELOG](https://github.com/ReeceM/h-bar/blob/master/CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](https://github.com/ReeceM/h-bar/blob/master/CONTRIBUTING.md) for details.

## Security

[SECURITY](https://github.com/ReeceM/h-bar/security/policy)

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [ReeceM](https://github.com/ReeceM)
- [All Contributors](../../contributors)

## Support

<a href="https://www.buymeacoffee.com/ReeceM" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>

[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/S6S7UQ66)

## License

The MIT License (MIT). Please see [License File](https://github.com/ReeceM/h-bar/blob/master/LICENSE.md) for more information.
14 changes: 14 additions & 0 deletions docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- _coverpage.md -->

<span style="font-size: 5rem;">📣</span>

# h-bar <small>v2.0</small>

> An Announcement Banner that is lightweight and customizable
- Simple and lightweight (~5.6kB gzipped)
- Customizable with personal templates
- Multiple default themes ready to go

[GitHub](https://github.com/reecem/h-bar/)
[Get Started](#h-bar-lightweight-announcement-bar)
48 changes: 39 additions & 9 deletions docs/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
<div id="h-bar"></div>
<div id="h-bar" style="position: -webkit-sticky; position: sticky; top: 0;" data-template="#hbar_template"></div>
<!-- HTML BELOW USED FROM: https://tailwindcomponents.com/component/simple-template -->
<div class="text-white mt-0 border border-blue-800 py-0 w-full h-full" style="background-image:url('https://demos.onepagelove.com/html/leno/images/header-background.jpg')">
<header class="flex md:justify-between p-8">
Expand Down Expand Up @@ -65,7 +65,39 @@ <h1 class="text-5xl font-bold uppercase text-center text-gray-300">Features</h1>
</div>
</section>
</div>
<script src="../dist/hBar.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/@reecem/h-bar@latest/dist/hBar.min.js"></script> -->
<script src="./scripts/hBar.min.js"></script>

<template id="hbar_template">
<div
class="hb-flex hb-w-full hb-flex-col md:hb-flex-row hb-text-sm hb-py-2 md:hb-px-20 hb-px-1 hb-items-center hb-justify-between hb-bg-indigo-400 hb-text-indigo-900">
<div class="hb-flex hb-items-center"><span
class="hb-px-2 hb-mx-2 hb-leading-relaxed hb-tracking-wider hb-uppercase hb-font-semibold hb-rounded-full hb-text-xs hb-bg-indigo-100 hb-text-indigo-900 shadow">New Release</span><a
class="hover:hb-underline hb-inline-flex hb-items-center hover:hb-text-indigo-100"
href="{% link %}" target="_blank">{% title %}
<svg class="hb-h-3 hb-w-3" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd"
d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
clip-rule="evenodd"></path>
</svg>
</a></div>
<div class="hb-flex hb-items-center">
<a class="hb-mx-5 hb-cursor-pointer hover:hb-underline hover:hb-text-red-100" href="http://docs.localhost.com">Info</a>
<a class="hb-mx-5 hb-cursor-pointer hover:hb-underline hover:hb-text-red-100" href="http://docs.localhost.com">Info</a>
</div>
</div>
<!-- <div class="absolute top-auto right-0 w-full flex justify-center mt-5">
<div class="max-w-lg">
<div class="bg-orange-100 border-l-4 border-orange-900 text-orange-700 p-4" role="alert">
<p class="font-bold">{% title %}</p>
<p>
<a href="{% link %}" class="hover:underline text-gray-700">Go to the GitHub Release</a>
</p>
</div>
</div>
</div> -->
</template>

<script>
/**
* Below we are using the h-bar with the preset values and not from a blog.
Expand All @@ -90,7 +122,7 @@ <h1 class="text-5xl font-bold uppercase text-center text-gray-300">Features</h1>
let github_release = (data) => {
const {name, html_url} = data[0];
return {
title: `Lateset version available ${name}`,
title: `Latest version available ${name}`,
link: html_url
};
}
Expand All @@ -99,17 +131,15 @@ <h1 class="text-5xl font-bold uppercase text-center text-gray-300">Features</h1>
/**
* Add two(2) minutes to the delay to see it work :)
*/
var dismissMillis = (new Date()).setMinutes(dateToday.getMinutes() + 2);
// var dismissMillis = (new Date()).setMinutes(dateToday.getMinutes() + 2);

hBar.init({
el: "#h-bar",
url: post_like_url,
onCompleted: (e) => {
console.log(e)
},
theme: 'lightGray',
headers: {
"Authorization": "Bearer 12345",
},
theme: 'red',
// dismissible: true,
// dismissFor: new Date(dismissMillis),
/**
Expand All @@ -128,7 +158,7 @@ <h1 class="text-5xl font-bold uppercase text-center text-gray-300">Features</h1>
}
]
})
hBar.fetchData()
hBar.fetch()
</script>
</body>
</html>
Loading

0 comments on commit e19fe36

Please sign in to comment.