项目作者: eldoy

项目描述 :
Javascript component library concept for single page applications
高级语言: JavaScript
项目地址: git://github.com/eldoy/brage.git
创建时间: 2018-07-03T09:44:57Z
项目社区:https://github.com/eldoy/brage

开源协议:MIT License

下载


Brage.js

Concept Javascript component library for single page web applications. Very minimal, only 150 lines of code, no dependencies, just plain vanilla Javascript (ES6). Does not use a virtual DOM, updates must be done by calling render when your data updates.

Includes complete webpack setup and tests. Enjoy!

INSTALLATION

npm i brage

You can also just include the javascript file found in dist/brage.js in your HTML file.

USAGE

From within your application views or components do for example import { div, a, p, input } from 'brage' to include the div, a, p and input tags.

To run the example application, clone the repo, and do npm install then npm run dev. The live server should start automatically in your browser at http://localhost:8080. Hot code reloading included out of the box.

Start the tests with npm run test. Testing brage views is super easy with Jest or JSDOM as everything is just a function.

  1. // This is what it looks like
  2. section(
  3. h1('Brage.js is so easy'),
  4. p('This is how you use it'),
  5. ul({ class: 'list' },
  6. li('Steak'),
  7. li('Milk'),
  8. li('Eggs'),
  9. li('Liver')
  10. ),
  11. aside('Cool?')
  12. )

API

Every element has access to its parent through the parent property.

  1. // Access the parent
  2. let world
  3. const list = ul(
  4. li('Hello'),
  5. world = li('World')
  6. )
  7. world.parent === list // true

The Brage DOM methods makes it easy to manipulate the DOM, but you can also just use the standard DOM methods included in all browsers. All Brage tag elements are just normal HTMLElements.

Mount

  1. // Mount list into body
  2. const list = ul(li('Hello'), li('World'))
  3. mount(list)
  4. // Mount list into another tag
  5. mount(list, document.querySelector('#app'))

Append

  1. // Append paragraph to body
  2. const paragraph = p('Hello world')
  3. append(paragraph)
  4. // Append paragraph to another element
  5. append(paragraph, document.querySelector('#app'))

Insert

  1. // Insert a new paragraph before the .paragraph element
  2. insert(p('New'), document.querySelector('.paragraph'))

Replace

  1. // Replace a paragraph with another paragaph
  2. replace(p('New'), document.querySelector('.paragraph'))

VIEWS

The views are the components of Brage. They should be a class or a function instance that has a render function.

  1. import { div, h1, p, img } from '@/modules/brage.js'
  2. import banner from '@/assets/images/brage.jpg'
  3. class HomeView {
  4. render = () => {
  5. return(
  6. div(
  7. h1('Home'),
  8. p('Welcome to our Brage.js demo page!'),
  9. div(
  10. img({ src: banner })
  11. )
  12. )
  13. )
  14. }
  15. }
  16. export default new HomeView()

Import the view in another file with

  1. import homeView from '@/views/site/home-view.js'

ROUTER

Brage comes with a router for your pages.

  1. // The view is a class that contains a render function.
  2. import { BrageRouter } from 'brage'
  3. import homeView from '@/views/site/home-view.js'
  4. import aboutView from '@/views/site/about-view.js'
  5. import listView from '@/views/site/list-view.js'
  6. import controllerView from '@/views/site/controller-view.js'
  7. import todoView from '@/views/todo/todo-view.js'
  8. const routes = [
  9. { path: '/', view: homeView },
  10. { path: '/about', view: aboutView },
  11. { path: '/list/:message', view: listView },
  12. { path: '/controller', view: controllerView },
  13. { path: '/todo', view: todoView }
  14. ]
  15. export default new BrageRouter(routes)

To add a route, use the router-link class for your link:

  1. import { header, nav, a, div } from '@/modules/brage.js'
  2. import routes from '@/lib/routes.js'
  3. class HeaderView {
  4. render = () => {
  5. const view = (
  6. header(
  7. nav(
  8. // Add the router-link class to each router link
  9. this.homeLink = a('Home', { class: 'router-link active', href: '/' }),
  10. this.aboutLink = a('About', { class: 'router-link', href: '/about' }),
  11. this.listLink = a('List', { class: 'router-link', href: '/list/hello' }),
  12. this.controllerLink = a('Controller', { class: 'router-link', href: '/controller' }),
  13. this.todoLink = a('Todo', { class: 'router-link', href: '/todo' })
  14. )
  15. )
  16. )
  17. // Call registerLinks for each link you want to use with the router
  18. routes.registerLinks([
  19. this.homeLink,
  20. this.aboutLink,
  21. this.listLink,
  22. this.controllerLink,
  23. this.todoLink
  24. ])
  25. return view
  26. }
  27. }
  28. export default new HeaderView()

Add a : to you link parts to make your link paths dynamic. /users/:id will match the link /users/1234 and will give you a props object sent to the render function in your view that looks like this:

  1. // Route props are sent to the render function
  2. render = (props) => {
  3. console.log(props) // { id: '1234' }
  4. }
  5. // Use destructuring to capture the props directly
  6. render = ({ id }) => {
  7. console.log(id) // '1234'
  8. }

TAGS

All HTML5 tags are supported. If you want to make your own tags for Web Components or similar, use t('tagname') instead.

Use fragment if you want to create a document fragment without any tag output.

a,
abbr,
address,
area,
article,
aside,
audio,
b,
base,
bdi,
bdo,
blockquote,
body,
br,
button,
canvas,
caption,
cite,
code,
col,
colgroup,
command,
datalist,
dd,
del,
details,
dfn,
div,
dl,
dt,
em,
embed,
fieldset,
figcaption,
figure,
footer,
form,
fragment,
h1,
h2,
h3,
h4,
h5,
h6,
head,
header,
hgroup,
hr,
html,
i,
iframe,
img,
input,
ins,
kbd,
keygen,
label,
legend,
li,
link,
main,
map,
mark,
menu,
meta,
meter,
nav,
noscript,
object,
ol,
optgroup,
option,
output,
p,
param,
pre,
progress,
q,
rp,
rt,
ruby,
s,
samp,
script,
section,
select,
small,
source,
span,
strong,
style,
sub,
summary,
sup,
table,
tbody,
td,
template,
textarea,
tfoot,
th,
thead,
time,
title,
tr,
track,
underline,
ul,
_var,
video,
wbr