Skip to content

Commit 4886b75

Browse files
committed
Local styles and scripts: Rewrite components to accept shared base, utilities, and config such as CSS class prefix
1 parent 462d976 commit 4886b75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4579
-3814
lines changed

alert.ts

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,85 @@
1-
import BaseComponent, { DATA_PREFIX } from './base-component'
2-
import EventHandler from './dom/event-handler'
3-
import { enableDismissTrigger } from './utilities/component-functions'
4-
import { defineJQueryPlugin } from './utilities'
5-
6-
/**
7-
* Constants
8-
*/
9-
10-
const NAME = 'alert'
11-
const DATA_KEY = `${DATA_PREFIX}.alert`
12-
const EVENT_KEY = `.${DATA_KEY}`
13-
14-
const EVENT_CLOSE = `close${EVENT_KEY}`
15-
const EVENT_CLOSED = `closed${EVENT_KEY}`
16-
const CLASS_NAME_FADE = 'fade'
17-
const CLASS_NAME_SHOW = 'show'
18-
19-
/**
20-
* Class definition
21-
*/
22-
23-
class Alert extends BaseComponent {
24-
// Getters
25-
static get NAME() {
26-
return NAME
27-
}
28-
29-
// Public
30-
close() {
31-
const closeEvent = EventHandler.trigger(this._element, EVENT_CLOSE)
32-
33-
if (closeEvent.defaultPrevented) {
34-
return
1+
export default function createAlert({
2+
BaseComponent,
3+
CLASS_PREFIX,
4+
DATA_PREFIX,
5+
enableDismissTrigger,
6+
EventHandler,
7+
}) {
8+
/**
9+
* Constants
10+
*/
11+
12+
const NAME = 'alert'
13+
const DATA_KEY = `${DATA_PREFIX}.alert`
14+
const EVENT_KEY = `.${DATA_KEY}`
15+
16+
const EVENT_CLOSE = `close${EVENT_KEY}`
17+
const EVENT_CLOSED = `closed${EVENT_KEY}`
18+
const CLASS_NAME_FADE = `${CLASS_PREFIX}fade`
19+
const CLASS_NAME_SHOW = `${CLASS_PREFIX}show`
20+
21+
/**
22+
* Class definition
23+
*/
24+
25+
class Alert extends BaseComponent {
26+
// Getters
27+
static get NAME() {
28+
return NAME
3529
}
3630

37-
this._element.classList.remove(CLASS_NAME_SHOW)
38-
39-
const isAnimated = this._element.classList.contains(CLASS_NAME_FADE)
40-
this._queueCallback(() => this._destroyElement(), this._element, isAnimated)
41-
}
42-
43-
// Private
44-
_destroyElement() {
45-
this._element.remove()
46-
EventHandler.trigger(this._element, EVENT_CLOSED)
47-
this.dispose()
48-
}
49-
50-
// Static
51-
static jQueryInterface(config) {
52-
return this.each(function () {
53-
const data = Alert.getOrCreateInstance(this)
31+
// Public
32+
close() {
33+
const closeEvent = EventHandler.trigger(this._element, EVENT_CLOSE)
5434

55-
if (typeof config !== 'string') {
35+
if (closeEvent.defaultPrevented) {
5636
return
5737
}
5838

59-
if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
60-
throw new TypeError(`No method named "${config}"`)
61-
}
39+
this._element.classList.remove(CLASS_NAME_SHOW)
6240

63-
data[config](this)
64-
})
65-
}
66-
}
41+
const isAnimated = this._element.classList.contains(CLASS_NAME_FADE)
42+
this._queueCallback(
43+
() => this._destroyElement(),
44+
this._element,
45+
isAnimated,
46+
)
47+
}
6748

68-
/**
69-
* Data API implementation
70-
*/
49+
// Private
50+
_destroyElement() {
51+
this._element.remove()
52+
EventHandler.trigger(this._element, EVENT_CLOSED)
53+
this.dispose()
54+
}
7155

72-
enableDismissTrigger(Alert, 'close')
56+
// Static
57+
static jQueryInterface(config) {
58+
return this.each(function () {
59+
const data = Alert.getOrCreateInstance(this)
60+
61+
if (typeof config !== 'string') {
62+
return
63+
}
64+
65+
if (
66+
data[config] === undefined ||
67+
config.startsWith('_') ||
68+
config === 'constructor'
69+
) {
70+
throw new TypeError(`No method named "${config}"`)
71+
}
72+
73+
data[config](this)
74+
})
75+
}
76+
}
7377

74-
/**
75-
* jQuery
76-
*/
78+
/**
79+
* Data API implementation
80+
*/
7781

78-
// defineJQueryPlugin(Alert)
82+
enableDismissTrigger(Alert, 'close')
7983

80-
export default Alert
84+
return Alert
85+
}

all.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { create } from './index'
2+
3+
// These can be imported individually
4+
import Alert from './alert'
5+
import Button from './button'
6+
import Carousel from './carousel'
7+
import Collapse from './collapse'
8+
import Dropdown from './dropdown'
9+
import Modal from './modal'
10+
import Offcanvas from './offcanvas'
11+
import Popover from './popover'
12+
import ScrollSpy from './scrollspy'
13+
import Tab from './tab'
14+
import Toast from './toast'
15+
import Tooltip from './tooltip'
16+
17+
create({
18+
components: {
19+
Alert,
20+
Button,
21+
Carousel,
22+
Collapse,
23+
Dropdown,
24+
Modal,
25+
Offcanvas,
26+
Popover,
27+
ScrollSpy,
28+
Tab,
29+
Toast,
30+
Tooltip,
31+
},
32+
})

base-component.ts

Lines changed: 0 additions & 92 deletions
This file was deleted.

base.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import Data from './dom/data'
2+
import EventHandler from './dom/event-handler'
3+
// import Config from './utilities/config'
4+
import { executeAfterTransition, getElement } from './utilities'
5+
6+
/**
7+
* Constants
8+
*/
9+
10+
const VERSION = '2024.09.18' // Fork of Bootstrap 5.3.3
11+
12+
export default function createBaseComponent({
13+
Config,
14+
DATA_PREFIX,
15+
DATA_PREFIX_BASE,
16+
}) {
17+
/**
18+
* Class definition
19+
*/
20+
21+
class BaseComponent extends Config {
22+
constructor(element, config) {
23+
super()
24+
25+
element = getElement(element)
26+
if (!element) {
27+
return
28+
}
29+
30+
this._element = element
31+
this._config = this._getConfig(config)
32+
33+
Data.set(this._element, this.constructor.DATA_KEY, this)
34+
}
35+
36+
// Public
37+
dispose() {
38+
Data.remove(this._element, this.constructor.DATA_KEY)
39+
EventHandler.off(this._element, this.constructor.EVENT_KEY)
40+
41+
for (const propertyName of Object.getOwnPropertyNames(this)) {
42+
this[propertyName] = null
43+
}
44+
}
45+
46+
_queueCallback(callback, element, isAnimated = true) {
47+
executeAfterTransition(callback, element, isAnimated)
48+
}
49+
50+
_getConfig(config) {
51+
config = this._mergeConfigObj(config, this._element)
52+
config = this._configAfterMerge(config)
53+
this._typeCheckConfig(config)
54+
return config
55+
}
56+
57+
// Static
58+
static getInstance(element) {
59+
return Data.get(getElement(element), this.DATA_KEY)
60+
}
61+
62+
static getOrCreateInstance(element, config = {}) {
63+
return (
64+
this.getInstance(element) ||
65+
new this(element, typeof config === 'object' ? config : null)
66+
)
67+
}
68+
69+
static get VERSION() {
70+
return VERSION
71+
}
72+
73+
static get DATA_PREFIX() {
74+
return DATA_PREFIX
75+
}
76+
77+
static set DATA_PREFIX(_prefix: string) {
78+
let prefix = _prefix.replace(/\-$/, '')
79+
DATA_PREFIX_BASE = prefix
80+
DATA_PREFIX = `${prefix}-`
81+
}
82+
83+
static get DATA_KEY() {
84+
return `${this.DATA_PREFIX}.${this.NAME}`
85+
}
86+
87+
static get EVENT_KEY() {
88+
return `.${this.DATA_KEY}`
89+
}
90+
91+
static eventName(name) {
92+
return `${name}${this.EVENT_KEY}`
93+
}
94+
}
95+
96+
return BaseComponent
97+
}

build/design.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/design.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)