Categories
React Answers

How to set the iframe content of a React component?

Sometimes, we want to set the iframe content of a React component.

In this article, we’ll look at how to set the iframe content of a React component.

How to set the iframe content of a React component?

To set the iframe content of a React component, we use the createPortal function.

For instance, we write

import React, { useState } from "react";
import { createPortal } from "react-dom";

export const IFrame = ({ children, ...props }) => {
  const [contentRef, setContentRef] = useState(null);
  const mountNode = contentRef?.contentWindow?.document?.body;

  return (
    <iframe {...props} ref={setContentRef}>
      {mountNode && createPortal(children, mountNode)}
    </iframe>
  );
};

to assign the setContentRef ref to the iframe.

Then we call createPortal with children and the iframe’s body element with contentRef?.contentWindow?.document?.body to populate it.

Conclusion

To set the iframe content of a React component, we use the createPortal function.

Categories
Angular Answers

How to set the Material date picker date format with Angular and TypeScript?

Sometimes, we want to set the Material date picker date format with Angular and TypeScript.

In this article, we’ll look at how to set the Material date picker date format with Angular and TypeScript.

How to set the Material date picker date format with Angular and TypeScript?

To set the Material date picker date format with Angular and TypeScript, we create a custom date adapter.

For instance, we write

const MY_DATE_FORMATS = {
  parse: {
    dateInput: { month: "short", year: "numeric", day: "numeric" },
  },
  display: {
    dateInput: "input",
    monthYearLabel: { year: "numeric", month: "short" },
    dateA11yLabel: { year: "numeric", month: "long", day: "numeric" },
    monthYearA11yLabel: { year: "numeric", month: "long" },
  },
};

to define a constant with the date formats.

Then we write

export class MyDateAdapter extends NativeDateAdapter {
  format(date: Date, displayFormat: Object): string {
    if (displayFormat === "input") {
      let day = date.getDate();
      let month = date.getMonth() + 1;
      let year = date.getFullYear();
      return this._to2digit(day) + "/" + this._to2digit(month) + "/" + year;
    } else {
      return date.toDateString();
    }
  }

  private _to2digit(n: number) {
    return ("00" + n).slice(-2);
  }
}

to return the date string in the format we want.

Then we write

//...

@NgModule({
  //...
  providers: [
    { provide: DateAdapter, useClass: MyDateAdapter },
    { provide: MD_DATE_FORMATS, useValue: MY_DATE_FORMATS },
  ],
  //...
})
export class AppModule {}

to add the adapter class and the object to our module.

Conclusion

To set the Material date picker date format with Angular and TypeScript, we create a custom date adapter.

Categories
JavaScript Answers

How to fix geocoding from address to latitude and longitude numbers not working with JavaScript?

Sometimes, we want to fix geocoding from address to latitude and longitude numbers not working with JavaScript.

In this article, we’ll look at how to fix geocoding from address to latitude and longitude numbers not working with JavaScript.

How to fix geocoding from address to latitude and longitude numbers not working with JavaScript?

To fix geocoding from address to latitude and longitude numbers not working with JavaScript, we use the Google Maps API.

For instance, we write

<script
  type="text/javascript"
  src="http://maps.google.com/maps/api/js?sensor=false"
></script>

to add the Google Maps API script.

Then we write

const geocoder = new google.maps.Geocoder();
const address = "new york";

geocoder.geocode({ address }, (results, status) => {
  if (status === google.maps.GeocoderStatus.OK) {
    const latitude = results[0].geometry.location.lat();
    const longitude = results[0].geometry.location.lng();
    console.log(latitude);
  }
});

to call geocode with the address to geocode the address and get the resulting latitude and longitude with lat and lng respectively.

Conclusion

To fix geocoding from address to latitude and longitude numbers not working with JavaScript, we use the Google Maps API.

Categories
JavaScript Answers

How to set pointer-events with JavaScript?

Sometimes, we want to set pointer-events with JavaScript.

In this article, we’ll look at how to set pointer-events with JavaScript.

How to set pointer-events with JavaScript?

To set pointer-events with JavaScript, we set the the style property value.

For instance, we write

document.getElementById("div").style["pointer-events"] = "auto";

to select the element with getElementById.

Then we set the style["pointer-events"] property toi set the pointer-events CSS property to 'auto'.

Conclusion

To set pointer-events with JavaScript, we set the the style property value.

Categories
JavaScript Answers

How to get the difference between two dates in years, months, days in JavaScript?

Sometimes, we want to get the difference between two dates in years, months, days in JavaScript.

In this article, we’ll look at how to get the difference between two dates in years, months, days in JavaScript.

How to get the difference between two dates in years, months, days in JavaScript?

To get the difference between two dates in years, months, days in JavaScript, we use the preciseDiff method.

For instance, we write

const starts = moment("2022-02-03 12:53:12");
const ends = moment();
const duration = moment.duration(ends.diff(starts));
const diff = moment.preciseDiff(starts, ends, true);
const diffHuman = moment.preciseDiff(starts, ends);

to get the duration between the starts and ends date with moment.duration and diff.

And then we call precisediff to get theg precise difference between the 2 dates.

Conclusion

To get the difference between two dates in years, months, days in JavaScript, we use the preciseDiff method.