Building an Image Carousel in React: A State-Driven Approach




 I'm diving deep into React development, and I'm currently building a simple website (yes, I know, very original!). One of the key elements I wanted to include was an image carousel in the hero section.

Now, I've built carousels before using plain JavaScript, and I initially approached this React project with the same mindset. I quickly encountered a crucial difference between the two: state management.

In vanilla JavaScript, I could easily manage the current image by simply updating a variable:



This function, attached to a button click event, would seamlessly cycle through the images.

Translating this to React JSX the code becomes:


The logic is good and working.

However, in React, this direct DOM manipulation doesn't play well with how React manages changes. React utilizes a virtual DOM, which efficiently tracks and updates only the necessary parts of the actual DOM.

To achieve smooth transitions in our React carousel, we need to leverage React's state management. Instead of directly modifying the displayed image, we can use the useState hook to manage the imageIndex within our React component:

The code looks like this:




Then we can add any transition to the images using css:



This approach, utilizing React's state management and conditional rendering, provides a more efficient and maintainable solution for building an image carousel compared to directly manipulating the DOM.

Make a animated mouse follower



Ever felt a website needed a touch of whimsy? Animated elements that react to user interaction can add a layer of fun and engagement. In this blog post, we'll delve into creating an animated "mouse follower" using the power trio of HTML, CSS, and JavaScript!




What is a Mouse Follower?

Imagine a playful element on your website that follows your mouse cursor around the screen. This is a mouse follower! It can be anything from a cute character to a subtle geometric shape, adding a dynamic touch to your design.

Here is how to do it:-

HTML-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<link
href="https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css"
rel="stylesheet"
/>
<title>mouse follower</title>
</head>
<body>
<div class="mouse-follower"> </div>
</body>
Thats all you need for the html. The real magic happens in css and js.


CSS:


.mouse-follower {
height: 2.1rem;
aspect-ratio: 1;
border-radius: 50%;
background: transparent;
border: 2.5px solid #6c99b2;
position: fixed;
left: 50;
top: 50;
translate: -50% -50%;
z-index: 900;
}


This uses manages the size of the mouse follower and gives it a nice circular shape and attention seeking border. The position fixed is the most important otherwise the element will not be able to follow the mouse properly.


JS:

const shape = document.querySelector(".mouse-follower");

document.body.onpointermove = (event) => {
let { clientX, clientY } = event;
shape.animate(
{
left: `${clientX}px`,
top: `${clientY}px`,
},
{ duration: 2000, fill: "forwards" }
);
};

Here we basically select the follower element and listen for the pointer move event. This pointer event is then passed in the left and top style of the element through the animate() method, which gives the element a slight lag animation.  

That's it. Now you have a playful little mouse follower which adds to the interactivity of your website.

DOM and BOM

In JavaScript, both DOM and BOM are crucial objects that provide access to different aspects of the web browsing experience. 


DOM (Document Object Model):

1. Focus: Represents the structure and content of the current web page (HTML document).
2. Structure: A tree-like hierarchy where each node represents an element in the HTML document.
3. Functionality: Allows you to manipulate the document, such as adding, removing, modifying elements, changing styles, and responding to user interactions.

Examples: Accessing elements by ID or class name, changing element content, adding event listeners to elements.

BOM (Browser Object Model):

 1. Focus: Provides access to the browser window, history, and other features beyond the specific document.
2. Structure: Not a tree, but rather a collection of objects representing various browser features.
3. Functionality: Allows you to interact with the browser itself, such as navigating to new pages, reading browser information (user agent, screen size), manipulating the browser window, and accessing history.

Examples: Opening a new window, accessing the user's location, checking the browser's screen resolution.

The Search for a Private Browser

 

The internet: a vast frontier, bursting with information and connection. But like the Wild West of old, it's a landscape fraught with peril. Our digital footprint is under constant scrutiny, leading to concerns about online privacy more relevant than ever.

In 2024, the internet's state reflects this double-edged sword. On one hand, information abundance empowers us. On the other, data tracking creates an unsettling feeling of being watched. From social media giants to targeted advertising, entities collect our information seemingly effortlessly, raising critical questions:

  • Who owns our data?
  • How is it used?
  • Are we truly in control?

The answer is often murky. This is where internet privacy steps in, crucial for protecting our identities and online behavior. Imagine your browsing history like a treasure map, and internet privacy safeguards act as the lock and key.

 

The importance of internet privacy extends beyond just personal information. It impacts:

  • Financial security: Data breaches can expose financial details, making us vulnerable to fraud.
  • Employment opportunities: Online profiles could influence hiring decisions, potentially creating bias.
  • Mental health: Targeted advertising can exploit vulnerabilities and contribute to anxiety or depression.

 So in this day and age a private and secure web browser is essential. But browsers claiming to be secure aren't fully secure. Browsers like Brave are great for the average consumer and they do really good work towards securing their browser. But if you are as paranoid about privacy as I am you might want to follow the steps mentioned below -

1. Install the latest version of firefox
2. Go to arkenfox gihub repo using (https://github.com/arkenfox/user.js)
3. Download the user.js file. (It's the last file in the explorer tree of the repo named 'user.js')
4. Open firefox and type 'about:profiles' in the address bar
5. This portion handles the profile you use when running firefox. Basically it handles all the browser settings.
6. Click on the 'create new profile' button and name it arkenfox.
7. Click on the 'open root folder' button on the newly created profile. It will open the root folder of the profile.
8. Delete all the files in that folder.
9. Move the downloaded user.js file into the folder
10. Restart firefox



Caution: It should be noted that all your extensions and data will be lost after restarting firefox, so backup all the data. Also your browser will behave very differently so read and understand all the settings the 'user.js' file will change before downloading and using it from the github repo. Do not attempt this if you don't understand what you're doing.

The reason I chose firefox is because as a dev and a guy loves linux operating systems, I am biased towards FOSS softwares. And firefox is more geared towards security than chromium based browsers.


If you've followed all the steps, Now you have a browser that is as hardened as it can be (in my opinion). All trackers and js will be blocked automatically. All you have to do is be a little cautious about entering shady sites and downloading untrusted files.


This doesn't block ads however. For that download extensions such as -
ublock origin - for adblocking
privacy badger - for better tracker protection etc.

Unlocking the Power of CSS Grid: Mastering Layouts for Stunning Web Designs

 



Welcome to the world of CSS Grid, where layout possibilities are limitless!** If you're ready to create sophisticated, responsive, and visually captivating web designs, this powerful CSS property is your key to success. Grab your toolkit and let's dive in!
 

What is CSS Grid?

Imagine a blank canvas divided into precise rows and columns, ready for you to arrange elements with unparalleled control. That's CSS Grid in a nutshell. It offers a two-dimensional layout system that empowers you to create intricate and adaptable web page structures with ease.



Key Concepts to Grasp:

- Grid Container: The parent element that defines the grid structure.
- Grid Items: The child elements positioned within the grid.
- Grid Lines: Horizontal and vertical lines that create grid tracks.
- Grid Tracks: The spaces between grid lines.
- Grid Cells: The individual units formed by grid tracks.
- Grid Areas: Named regions within the grid, combining multiple cells.
 

Harnessing the Grid Power: 

Basic Setup

1. Activate the Grid: Set the `display` property of the grid container to `grid`.
2. Define Rows and Columns: Use `grid-template-columns` and `grid-template-rows` to create the grid structure.
 

Example:

.grid-container {
  display: grid;
  grid-template-columns: 200px 1fr 200px; /* Three columns: fixed, flexible, fixed */
  grid-template-rows: repeat(4, 100px); /* Four rows of equal height */
}


Positioning Grid Items:

- Explicit Placement: Control item placement with properties like `grid-column-start`, `grid-column-end`, `grid-row-start`, and `grid-row-end`.
- Naming Grid Areas: Assign names to grid areas and use `grid-area` to position items.
 

Manipulating Grid Items:

- Alignment: Use `justify-items` and `align-items` for horizontal and vertical alignment within cells.
- Distribution: Use `justify-content` for horizontal distribution of items within the grid container.
- Spanning Multiple Cells: Use `grid-column-end`, `grid-row-end`, or the shorthand `grid-area`.


Ready to Flex Your Grid Muscles?

CSS Grid offers a vast range of possibilities for crafting dynamic and visually engaging layouts. Explore its features, experiment with different approaches, and unleash your creativity!
 

Additional Tips:

- Visualize your grid structure with tools like Firefox's Grid Inspector or Chrome's DevTools.
- Utilize grid-specific properties to fine-tune your layouts.
- Consider using grid templates for common patterns.
- Embrace responsiveness with grid media queries.




What is DOM?

 

The Power Javascript brings to the table is DOM manipulation. But what is DOM? DOM is read as Document Object Model. It is a hierarchical tree like structure which depicts the entire webpage. It has different Nodes as its branches and they have a parent-child relation. Modern web browsers are able to read this structure and make changes accordingly. With Javascript one basically has the power to manipulate this structure.

 

 

 

Here the window is the root of the tree. The window provides us with predefined objects and methods. For example, console is an object provided by the window element. One can use this object to log something to the console by typing,

window.console.log('your message') 

The document is the parent element of all your html content and the one you'll be working with the most as a web dev. This gives us access to the html using document.body [targets the body of html], document.head [targets the head of html] etc. Some of you may also be familiar with the methods this provides such as Document.queryselector() and Document.getElementById().

In conclusion, DOM is a tree-like structure that is present on every webpage. Javascript gives is the ability to access with by targeting different objects and by utilizing various methods.

Vim basics



 


Vim is a highly configurable text editor that is known for its efficiency and power. It was designed as a more advanced version of the vi editor, which was included in Unix systems. Vim is available on a wide variety of platforms, including Linux, MacOS, and Windows. It is known for its modal editing, which allows users to switch between different modes for different tasks, such as inserting text, navigating the document, and executing commands. Vim also has a large number of built-in features and can be extended with plugins to add even more functionality.

I recently started learning vim and I think it has made writing code more enjoyable for me. So I'm sharing with you all so you can give it a try.

The most important thing to know is that vim has three modes

  1. Normal mode - Usually used to move around in a file
  2. Insert mode - Used when editing and writing code
  3. Visual mode - Used for selecting and manipulating code

Here are some basic vim commands:

1. Cursor Movement

  • Use the h, j, k, and l keys to move the cursor left, down, up, and right, respectively. [ like using arrow keys]

  • Use the 0 (zero) key to move the cursor to the beginning of the line.

  • Use the $ key to move the cursor to the end of the line.

  • Use the G key to move the cursor to the end of the file.

  • Use the gg key to move the cursor to the beginning of the file.

  • Use the % key to jump to matching brackets or parentheses.

  • Use the f key followed by a character to jump to the next occurrence of that character.

  • Use the F key followed by a character to jump to the previous occurrence of that character.

  • Use the t key followed by a character to jump to the character before the next occurrence of that character.

  • Use the T key followed by a character to jump to the character after the previous occurrence of that character.

2. Insert Mode actions
  • i - insert before the cursor

  • I - insert at the beginning of the line

  • a - insert (append) after the cursor

  • A - insert (append) at the end of the line

  • o - append (open) a new line below the current line

  • O - append (open) a new line above the current line


3. Editing
  • dd - delete the current line

  • D - delete from the cursor to the end of the line

  • x - delete the character under the cursor

  • X - delete the character before the cursor

  • p - paste the clipboard after the cursor

  • P - paste the clipboard before the cursor

  • u - undo the last change

  • U - undo all changes to the current line

  • . - repeat the last change

  • :w - write (save) the changes to the file

  • :q - quit Vim

  • :wq - write (save) the changes and quit Vim

  • :q! - quit Vim and discard all changes


More vim resources:

Web Development Essential Concepts


  1. Working principle of internet
  2. What is Https
  3. IP adress
  4. World Wide Web
  5. URL
  6. Hardware
  7. Software
  8. Web Browsers
  9. Web Servers
  10. Domains and DNS
  11. Html
  12. Css (optionally css frameworks)
  13. JavaScript (optionally js frontend frameworks and libraries)
  14. Links
  15. Accessibility
  16. Browser Events and APIs
  17. NodeJS
  18. NPM
  19. Error status codes
  20. SEO
  21. Website Optimization
  22. Cloud Hosting
  23. Web Security
  24. Site Maintenance
Web Dev is a tricky skill to learn and one may never be able to fully learn it. But learning these concepts will at the very least get you started on the right track.

Virtualization (QEMU).

In their own words, "QEMU is a generic and open source machine emulator and virtualizer." 

QEMU can use other hypervisors like Xen or KVM to use CPU extensions (HVM) for virtualization. When used as a virtualizer, QEMU achieves near native performances by executing the guest code directly on the host CPU. 

In simple terms, one can use a different opearating system on top of another operating system whilst achieving real hardware performance.

Another way to use QEMU is User Mode Emulation, where QEMU can launch processes compiled for one CPU on another CPU. In this mode the CPU is always emulated.

 The awesome thing is QEMU is supported on both Windows and Linux (including macos). In my personal experience it works best in Linux rather than windows.

 

Required packages for QEMU on Linux :

Arch:

 



sudo pacman -S qemu virt-manager virt-viewer dnsmasq vde2 bridge-utils openbsd-netcat ebtables iptables libguestfs

 
Debian:
 

 
sudo apt install qemu-kvm qemu-system qemu-utils python3 python3-pip libvirt-clients  libvirt-daemon-system bridge-utils virtinst libvirt-daemon virt-manager

 
For full setup of QEMU refer to - 
1. https://wiki.archlinux.org/title/QEMU
2. https://christitus.com/vm-setup-in-linux/
 
Windows:
The windows installation is a bit tricky since it requires you 
to partition your drive and install it on that partition as you 
would an operation system.
A comprehensive guide for windows can be found at
 https://www.minitool.com/partition-disk/qemu-for-windows.html 
 
In conclusion QEMU allows you to run different OS's on a single host machine
 without any performance bumps so it is a great alternative to dual booting. 
The one thing to keep in mind is it uses large amount of system resources. 
So it may affect system with low specs negatively. 
 



 

What is Docker?

 


Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. It basically bundles all your project as well as its dependencies so anyone can use it in any device. This basically solves any version incompatibility problems which is more prevalent than one thinks. 

How Docker works?

Docker uses containers under the hood. A container is a sandboxed process running on a host machine that is isolated from all other processes running on that host machine. 

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon.

To summarize, a container:

  • Is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI.
  • Can be run on local machines, virtual machines, or deployed to the cloud.
  • Is portable (and can be run on any OS).
  • Is isolated from other containers and runs its own software, binaries, configurations, etc.

 

Most used docker commands:

 1. docker run - Creates an image

 2. docker ps - Lists all active images

 3. docker images - Lists all local images

 4. docker rm - Deletes container

 5. docker build - Constructs new docker image (needs a dockerfile: https://docs.docker.com/engine/reference/builder/) 

In conclusion, docker has become essential for modern day development be it a personal project or group project. It makes life way easier in my opinion, especially for large scale projects.