Git Product home page Git Product logo

dom-window-manager's Introduction

DOM Window Manager logo

DOM Window Manager

A simple window manager for DOM elements

Buy Me a Coffee at ko-fi.com

Demo 1

Demo 2

Repository

npm package

What it does

  1. Makes DOM elements draggable
  2. Moves an element on top of all other draggable elements (ex. when clicked)

Description

DOM Window Manager consists of a single class, WindowManager, and a set of utility functions. WindowManager uses the singleton pattern, effectively restricting the instantiation of the class to a singular instance to be used by all components, in any file.

Instructions

At first, you need to import WindowManager and dragElement in each file that you create elements that you want to use with the DOM Window Manager. WindowManager is the class that manages the elements and dragElement is the utility function that makes elements draggable.

import { WindowManager, dragElement }  from "dom-window-manager";

Then, you have to instanciate the window manager in each file as well.

let windowManager = new WindowManager();

It takes an optional parameter, base, that is the initial z-index value that all elements will have. If no value is provided, base defaults to 1.

Then, at some point you will create an element

const element = document.createElement("div");

It could be any type of element (p, h1, div etc.). The only prerequisite, is to set its position attribute to absolute. It can be done either in JavaScript of in the CSS file.

element.style.position = "absolute";

You can now call the dragElement function, with the element as its parameter.

dragElement(element);

You also need to set the elements z-index value to the base provided by WindowManager.

element.style.zIndex = windowManager.base;

Finally, you will probably want to make the element come on top of all other elements when clicked.

element.addEventListener("mousedown", () => {
    element.style.zIndex = windowManager.moveOnTop();
});

As you have seen, DOM Window Manager takes a very minimalist approach, providing you with the bare necessities, and giving you absolute freedom to use it as you see fit for your project.

All the code

import { WindowManager, dragElement }  from "dom-window-manager";

let windowManager = new WindowManager();
const element = document.createElement("div");
element.style.position = "absolute";
dragElement(element);
element.style.zIndex = windowManager.base;

element.addEventListener("mousedown", () => {
    element.style.zIndex = windowManager.moveOnTop();
});

๐Ÿ’– Support the Project

Thank you so much for your interest in my project! If you want to go a step further and support my open source work, buy me a coffee:

Buy Me a Coffee at ko-fi.com

License

Copyright (c) 2023 Michael Kolesidis
Licensed under the GNU Affero General Public License v3.0.

dom-window-manager's People

Contributors

michaelkolesidis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

yassineelo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.