mirror of
https://github.com/locomotivemtl/locomotive-boilerplate.git
synced 2026-01-15 00:55:08 +08:00
Add custom grid helper based on CSS custom properties
This commit is contained in:
committed by
Deven Caron
parent
9219a4cc0a
commit
b7c49086c9
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"version": 1665079086854
|
||||
"version": 1667240111168
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export {default as Example} from './modules/Example';
|
||||
export {default as GridHelper} from './modules/GridHelper';
|
||||
export {default as Load} from './modules/Load';
|
||||
export {default as Scroll} from './modules/Scroll';
|
||||
|
||||
89
assets/scripts/modules/GridHelper.js
Normal file
89
assets/scripts/modules/GridHelper.js
Normal file
@@ -0,0 +1,89 @@
|
||||
import { module } from 'modujs';
|
||||
|
||||
export default class GridHelper extends module {
|
||||
|
||||
static get settings() {
|
||||
return {
|
||||
GUTTER: 'var(--grid-gutter, 0)',
|
||||
MARGIN: 'var(--grid-margin, 0)',
|
||||
COLOR: 'rgba(255, 0, 0, .1)',
|
||||
}
|
||||
}
|
||||
|
||||
constructor(m) {
|
||||
super(m);
|
||||
}
|
||||
|
||||
init() {
|
||||
this.setColumns()
|
||||
this.setStyles()
|
||||
this.bindEvents()
|
||||
|
||||
window.addEventListener('resize', this.onResize = () => this.setColumns())
|
||||
}
|
||||
|
||||
setColumns() {
|
||||
|
||||
// Clear columns
|
||||
this.el.innerHTML = ''
|
||||
|
||||
// Loop through columns
|
||||
const columns = Number(window.getComputedStyle(this.el).getPropertyValue('--grid-columns'))
|
||||
|
||||
let $col
|
||||
for (var i = 0; i < columns; i++) {
|
||||
$col = document.createElement('div')
|
||||
$col.style.flex = '1 1 0'
|
||||
$col.style.backgroundColor = GridHelper.settings.COLOR
|
||||
this.el.appendChild($col)
|
||||
}
|
||||
}
|
||||
|
||||
setStyles() {
|
||||
const elStyles = this.el.style
|
||||
elStyles.zIndex = '10000'
|
||||
elStyles.position = 'fixed'
|
||||
elStyles.top = '0'
|
||||
elStyles.left = '0'
|
||||
elStyles.display = 'flex'
|
||||
elStyles.width = '100%'
|
||||
elStyles.height = '100%'
|
||||
elStyles.columnGap = GridHelper.settings.GUTTER
|
||||
elStyles.paddingLeft = `calc(${GridHelper.settings.MARGIN} + ${GridHelper.settings.GUTTER})`
|
||||
elStyles.paddingRight = `calc(${GridHelper.settings.MARGIN} + ${GridHelper.settings.GUTTER})`
|
||||
elStyles.pointerEvents = 'none'
|
||||
elStyles.visibility = 'hidden'
|
||||
}
|
||||
|
||||
bindEvents() {
|
||||
|
||||
let ctrlDown = false
|
||||
let isActive = false
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if(e.key == 'Control') {
|
||||
ctrlDown = true;
|
||||
} else {
|
||||
if(ctrlDown && e.key == 'g') {
|
||||
|
||||
if(isActive) {
|
||||
this.el.style.visibility = 'visible'
|
||||
} else {
|
||||
this.el.style.visibility = 'hidden'
|
||||
}
|
||||
|
||||
isActive = !isActive
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
document.addEventListener('keyup', (e) => {
|
||||
if(e.key == 'Control') {
|
||||
ctrlDown = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
destroy() {
|
||||
window.removeEventListener('resize', this.onResize)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,22 @@
|
||||
// Elements / Document
|
||||
// ==========================================================================
|
||||
|
||||
:root {
|
||||
|
||||
// Grid
|
||||
--grid-columns : 4;
|
||||
--grid-gutter : #{rem(10px)};
|
||||
--grid-gutter-half : calc(0.5 * var(--grid-gutter));
|
||||
--grid-margin : 0px;
|
||||
|
||||
@media (min-width: $from-small) {
|
||||
--grid-columns : 12;
|
||||
--grid-gutter : #{rem(16px)};
|
||||
--grid-margin : #{rem(20px)};
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Simple page-level setup.
|
||||
//
|
||||
// 1. Include web fonts
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -41,7 +41,7 @@
|
||||
|
||||
<body data-module-load>
|
||||
|
||||
<!-- <p aria-hidden="true" class="u-screen-reader-text" style="font-family: 'Webfont';"> </p> -->
|
||||
<div data-module-grid-helper></div>
|
||||
|
||||
<div data-load-container>
|
||||
<div class="o-scroll" data-module-scroll="main">
|
||||
|
||||
Reference in New Issue
Block a user