Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions CONSTANTS_REFACTOR_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Allonsh Constants Refactor Summary

## Overview

This document summarizes the refactoring work done to replace hardcoded values with meaningful constants throughout the Allonsh library.

## Files Created/Modified

### 1. New Constants File: `src/constants.js`

- **Z_INDEX**: Centralized z-index values (GHOST: 999, DRAGGING: 1000, DROPPED: 9999, etc.)
- **DEFAULTS**: Default configuration values (stack spacing, direction)
- **CSS_CLASSES**: Consistent CSS class names
- **CSS_POSITIONS**: Position values (relative, absolute)
- **CSS_CURSORS**: Cursor values (grab, grabbing)
- **OPACITY**: Opacity values (ghost: 0.3, full: 1)
- **EVENTS**: Custom event names
- **FLEX_DIRECTIONS**: Flexbox direction values
- **DISPLAY_MODES**: Display property values
- **POINTER_EVENTS**: Pointer-events values

### 2. Main Library: `src/allonsh.js`

**Replaced hardcoded values with constants:**

#### Z-Index Values:

- `999` → `Z_INDEX.GHOST`
- `1000` → `Z_INDEX.DRAGGING`
- `9999` → `Z_INDEX.DROPPED`

#### Default Values:

- `'horizontal'` → `DEFAULTS.STACK_DIRECTION`
- `5` → `DEFAULTS.STACK_SPACING`

#### CSS Class Names:

- `'allonsh-dropzone'` → `CSS_CLASSES.DROPZONE`
- `'allonsh-highlight'` → `CSS_CLASSES.HIGHLIGHT`
- `'allonsh-draggable'` → `CSS_CLASSES.DRAGGABLE`
- `'restricted'` → `CSS_CLASSES.RESTRICTED`

#### CSS Properties:

- `'relative'` → `CSS_POSITIONS.RELATIVE`
- `'absolute'` → `CSS_POSITIONS.ABSOLUTE`
- `'grab'` → `CSS_CURSORS.GRAB`
- `'grabbing'` → `CSS_CURSORS.GRABBING`
- `'flex'` → `DISPLAY_MODES.FLEX`
- `'row'` → `FLEX_DIRECTIONS.ROW`
- `'column'` → `FLEX_DIRECTIONS.COLUMN`
- `'wrap'` → `FLEX_WRAP`

#### Opacity Values:

- `'0.3'` → `OPACITY.GHOST`
- `'1'` → `OPACITY.FULL`

#### Pointer Events:

- `'none'` → `POINTER_EVENTS.NONE`
- `'auto'` → `POINTER_EVENTS.AUTO`

#### Event Names:

- `'allonsh-dragstart'` → `EVENTS.DRAG_START`
- `'allonsh-drop'` → `EVENTS.DROP`
- `'allonsh-dragenter'` → `EVENTS.DRAG_ENTER`
- `'allonsh-dragleave'` → `EVENTS.DRAG_LEAVE`

### 3. Demo Script: `demo/js/script.js`

- Imported constants for default values
- Replaced hardcoded `10` with `DEFAULTS.STACK_SPACING_DEMO`

### 4. Test File: `test/allonsh.test.js`

- Imported constants for event names
- Replaced hardcoded event name with `EVENTS.DROP`

### 5. CSS File: `demo/css/styles.css`

- Added comments indicating z-index values could be updated to use CSS custom properties
- Values remain hardcoded for now but are documented for future refactoring

## Benefits Achieved

1. **Maintainability**: All hardcoded values are now centralized in one location
2. **Consistency**: Values are guaranteed to be consistent across the codebase
3. **Self-documenting**: Constants have meaningful names that explain their purpose
4. **Easy Updates**: Changing a value requires updating only the constants file
5. **Type Safety**: Better IDE support and error detection
6. **Code Clarity**: Intent is clearer with named constants instead of magic numbers

## Future Improvements

1. **CSS Custom Properties**: Z-index values in CSS could be moved to CSS custom properties
2. **Theme System**: Constants could be extended to support theme-based values
3. **Configuration**: Constants could be made configurable at runtime
4. **Validation**: Add validation to ensure constant values are within expected ranges

## Impact

- **High Priority**: This was a quick win with massive maintainability improvement
- **Low Risk**: Changes are purely internal refactoring with no API changes
- **High Value**: Makes the codebase significantly easier to maintain and extend
10 changes: 5 additions & 5 deletions demo/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ body {
/* Header */
.demo-header {
position: fixed;
z-index: 10;
z-index: 10; /* This could be updated to use CSS custom properties if needed */
box-shadow: 1px 2px 5px rgba(0, 0, 0, 0.5);
display: flex;
justify-content: space-between;
Expand Down Expand Up @@ -226,7 +226,7 @@ body.night .demo-section__playground {
padding: 0.6rem;
border-radius: 50%;
cursor: pointer;
z-index: 1000;
z-index: 1000; /* This could be updated to use CSS custom properties if needed */
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

Expand Down Expand Up @@ -287,7 +287,7 @@ body.night .demo-section__playground {
background-color: var(--bg-color);
padding: 1rem;
left: 1.5rem;
z-index: 5;
z-index: 5; /* This could be updated to use CSS custom properties if needed */
font-size: 1.2rem;
font-weight: 600;
border: 2px solid var(--panel-border);
Expand Down Expand Up @@ -427,7 +427,7 @@ body.night .demo-section__playground {
border-radius: 1rem;
cursor: pointer;
transition: background-color 0.3s;
z-index: 9;
z-index: 9; /* This could be updated to use CSS custom properties if needed */
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

Expand All @@ -445,7 +445,7 @@ body.night .demo-section__playground {
position: fixed;
bottom: 1rem;
left: 1rem;
z-index: 9;
z-index: 9; /* This could be updated to use CSS custom properties if needed */
transform: translateX(-120%);
transition: transform 0.3s ease;
}
Expand Down
6 changes: 4 additions & 2 deletions demo/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ let enableStackingBool = true;
let restrictDropzoneBool = true;

let ghostEffectBool = true;
let stackSpacingDefault = 10;
import { DEFAULTS } from '../../src/constants.js';

let stackSpacingDefault = DEFAULTS.STACK_SPACING_DEMO;

function initAllonsh(options) {
if (allonshInstance) {
Expand Down Expand Up @@ -142,7 +144,7 @@ resetBtn.addEventListener('click', () => {
enableStackingBool = true;
restrictDropzoneBool = true;
ghostEffectBool = true;
stackSpacingDefault = 10;
stackSpacingDefault = DEFAULTS.STACK_SPACING_DEMO;
currentStackDirection = 'horizontal';
setDefaultsOnLoad();
updateStackDirectionButtons();
Expand Down
Loading