Issue with Angular 6 Snap-to-Component Directive's Functionality in Chrome Browser

I have developed a Snap-to-Component Directive that functions perfectly in Firefox but encounters issues in Chrome. I have checked canIUse and it should be compatible, so I am seeking insights, especially regarding cross-browser compatibility. Your input is highly appreciated. Thank you.

Directive code snippet (windowsnap.directive.ts):

import {Directive, Input, HostListener, ElementRef} from '@angular/core';

@Directive({
  selector: '[windowsnap]'
})
export class WindowSnapDirective {

  scrollHistory = [];
  parent = this.el.nativeElement;

  constructor(private el: ElementRef) { }

  closest(num, arr) {
    let curr = arr[0];
    arr.forEach((val) => {
        if (Math.abs(num - val) < Math.abs(num - curr)) {
          curr = val;
        } 
    });
    return curr;
  }


 @HostListener('mouseup') onMouseUp(){
    this.scrollHistory.unshift(this.parent.scrollLeft);

    if(this.parent.scrollLeft === this.scrollHistory[1]) { return; }

    console.log(this.scrollHistory);

    let child1El = this.parent.querySelector('child1');
    let child2El = this.parent.querySelector('child2');
    let child3El = this.parent.querySelector('child3');

    let child1LeftBoundary = child1El.offsetLeft;
    let child1MiddleBoundary = child1El.offsetWidth * 0.5 + child1LeftBoundary;
    let child1RightBoundary = child1El.offsetWidth + child1LeftBoundary;

    let child2LeftBoundary = child2El.offsetLeft;
    let child2MiddleBoundary = child2El.offsetWidth * 0.5 + child2LeftBoundary;
    let child2RightBoundary = child2El.offsetWidth + child2LeftBoundary;

    let child3LeftBoundary = child3El.offsetLeft;
    let child3MiddleBoundary = child3El.offsetWidth * 0.5 + child3LeftBoundary;
    let child3RightBoundary = child3El.offsetWidth + child3LeftBoundary;

    let viewBoxL = this.parent.scrollLeft;
    let viewBoxC = this.parent.scrollLeft + (this.parent.offsetWidth * 0.5);
    let viewBoxR = this.parent.scrollLeft + this.parent.offsetWidth;

    let a = (viewBoxC - child1MiddleBoundary);
    let b = (viewBoxC - child2MiddleBoundary);
    let c = (viewBoxC - child3MiddleBoundary);

    let closestChildMid = this.closest(0, [a, b, c]);

    if(closestChildMid === a) {
      child1El.scrollIntoView({behavior: "smooth", block: "center"});
    }
    if(closestChildMid === b) {
      child2El.scrollIntoView({behavior: "smooth", block: "center"});
    }
    if(closestChildMid === c) {
      child3El.scrollIntoView({behavior: "smooth", block: "center"});
    }
  }
}

Any assistance is valued. Thank you!

Answer №1

After exporting the stackblitz project and testing it locally, the compatibility issue I was facing seems to have disappeared. It appears to have been a problem with stackblitz itself. I will consider submitting a bug report to the platform.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The ipcRenderer is failing to be activated

Looking to establish IPC communication between my React component and the main Electron process. Check out the code snippet below: export default class A extends React.Component{ ..... openFile = () => { console.log('test'); ipcRende ...

Enhance user experience with Bootstrap by automatically adding a frame around a card upon clicking

Hello everyone! I am a beginner in the Angular/Web Development world and I am currently working on improving my HTML/CSS skills. While using Bootstrap in my Angular project, I ran into a challenge that I couldn't figure out on my own. I have implement ...

We were unable to identify any Next.js version in your project. Please ensure that the `"next"` package is installed in either the "dependencies" or "devDependencies" section

My attempt to deploy a Next app using the Vercel CLI has hit a roadblock. After running vercel build with no errors, I proceeded to deploy with vercel deploy --prebuilt, which also went smoothly. However, when trying to move the project from the preview en ...

Typescript: Subscribed information mysteriously disappeared

[ Voting to avoid putting everything inside ngOnit because I need to reuse the API response and model array in multiple functions. Need a way to reuse without cluttering up ngOnInit. I could simply call subscribe repeatedly in each function to solve the p ...

Insert a blank row at the top of the grid in Wijmo for Angular 2

I am attempting to insert a new empty row at the start of the grid when an external button is clicked. The grid is displaying correctly. <wj-flex-grid #flex [itemsSource]="data" [isReadOnly]="true" [headersVisibility]="'Column' ...

Transformed Vue code with TypeScript using a more aesthetically pleasing format, incorporating ref<number>(0)

Original Format: const activeIndex = ref<number>(0) Formatted Version: const activeIndex = ref < number > 0 Prettier Output: click here for image description Prettier Configuration: { "$schema": "https://json.schemastore.org ...

What is the process for making a POST request with the Google Chrome Puppeteer library?

Hello everyone, I'm currently attempting to make a POST request using the puppeteer headless chrome library. I'm running into some issues with the code below and can't seem to figure out what's wrong. // Obtain the csrf token ...

Challenges in merging

I'm running into issues with my Angular project. Can anyone lend a hand? ERROR in src/app/app-routing.module.ts(5,1): error TS1185: Merge conflict marker encountered. ...

Is there a way to programmatically add a timestamp to a form in Angular6?

Is there a way to automatically populate new forms with the current datetime value? this.editForm.patchValue({ id: chatRoom.id, creationDate: chatRoom.creationDate != null ? chatRoom.creationDate.format(DATE_TIME_FORMAT) : null, roo ...

Eliminate unnecessary HTML elements in Angular

I am currently using ngSwitchCase for 3 different cases, and I have noticed that I am duplicating the same code inside each case. Is there a way to eliminate this redundancy? The redundant code is located within the "app-device" component <div *ngS ...

Challenge Encountered while Generating Angular Docker Image using VSTS Pipeline

I'm currently in the process of setting up a VSTS pipeline to create a Docker Image for an Angular Application. I've chosen the "Hosted Windows Container" as the Agent pool, but I'm encountering the following error: Step 1/5: FROM nginx:alp ...

Error: The script cannot be compiled with the '--isolatedModules' flag as it is recognized as a global script file

Currently, I am working on building my NextJS app and incorporating Cypress for testing along with Typescript. During this process, I encountered the following error: Type error: 'home.cy.ts' cannot be compiled under '--isolatedModules' ...

Unlock the Power of EmailJS with Vue.js 2 and TypeScript

I couldn't find a similar issue online, so here's my problem. I'm trying to create a form for receiving contact from an app using Vue.js 2 and TypeScript. Here is my code: <form ref="form" class="form-data" @submit.pr ...

Error: Cannot locate 'import-resolver-typescript/lib' in jsconfig.json file

Issue: An error occurred stating that the file '/Users/nish7/Documents/Code/WebDev/HOS/frontend/node_modules/eslint-import-resolver-typescript/lib' could not be found. This error is present in the program because of the specified root file for c ...

A guide on validating dates in Angular Ionic5 with the help of TypeScript

I have tried multiple solutions, but none seem to work when validating the current date with the date entered by the user. The date is passed from the user into the function parameters, but how do I perform validation? How can I validate the date? isToday( ...

Having difficulty compiling Angular with Bootstrap

I have encountered an error while attempting to start a project with Angular: "ERROR in multi ./src/styles.css ./node_modules/bootstrap/dist/css/bootstrap.min.css Module not found:" Here are the dependencies: "dependencies": { "@angular/animation ...

What is the purpose of RouterLink and RouterOutlet in Angular?

RouterLink and RouterOutlet are both angular attribute directives that assist in navigating to a specific route from the collection of routes. Once a matching route is found, the corresponding component is loaded, and its template is rendered on the browse ...

Having trouble getting useFieldArray to work with Material UI Select component

I am currently working on implementing a dynamic Select field using Material UI and react-hook-form. While the useFieldArray works perfectly with TextField, I am facing issues when trying to use it with Select. What is not functioning properly: The defau ...

When I select a link on the current page, I would like the information in the input fields to be cleared

Currently using Angular 8, I recently included onSameUrlNavigation: 'reload' to my router. This change has successfully allowed the page to reload upon a second click on the same link. However, I've noticed that the input fields on the reloa ...

Leveraging ngModel within Form Arrays in Angular 4 with ReactiveFormsModule

While working with simple array forms in Angular 4, I encountered an unusual problem with ngModel ...