Coder Social home page Coder Social logo

ionic-print-test's People

Contributors

btsiders avatar dependabot[bot] avatar

Watchers

 avatar  avatar

ionic-print-test's Issues

EventListener to set shadow-root styles

Thank you very much, you already helped me very much. Maybe we can put your approach into an npm-package?

I created an event listener, to set position property:

if (window.matchMedia) {
  const mediaQueryList = window.matchMedia('print');
  mediaQueryList.addEventListener('change', (mql) => {
    if (mql.matches) {
      document.querySelectorAll('ion-content').forEach((element) => {
        const scroll = element.shadowRoot.querySelector('.inner-scroll') as HTMLElement;
        scroll.style.position = 'relative';
      });
    } else {
      document.querySelectorAll('ion-content').forEach((element) => {
        const scroll = element.shadowRoot.querySelector('.inner-scroll') as HTMLElement;
        scroll.style.removeProperty('position');
      });
    }
  });
}

Also I extended the styles because I had some problems with (ion-tabs, ion-split-pane):

body {
  position: relative !important;
  max-width: unset !important;
  height: unset !important;
  max-height: unset !important;
  overflow: visible; /* enable scroll on body */
}

ion-app,
ion-router-outlet,
ion-content,
ion-split-pane,
ion-tabs,
.ion-page:not(.ion-page-hidden),
.tabs-inner {
  position: relative !important;
  contain: unset !important;
  display: block !important;
  overflow: unset !important; /* remove default overflow hidden */
}

Directive to solve the problem

Dear @btsiders,
I would like first to thank you for pointing me in the right direction. I had the same issue when printing an Ionic app. I totally agree with your solution: adding a CSS variable would not break anything and will solve the problem elegantly.

For the meantime, I have written a directive (the javascript way) that will make an ion-content printable. Anyone can add this directive to app.module.ts (or to a shared module):

  declarations: [
    PrintableDirective,
    ...
  ],

Then you can use it as such:
<ion-content appPrintable>

The directive code is following:

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

@Directive({
    selector: 'ion-content[appPrintable]'
})
export class PrintableDirective implements OnInit {
    constructor(private el: ElementRef) {
    }

    ngOnInit(count = 0) {
        if (count > 10) {
            return;
        }
        // if ion-content is not initilized yet, re-run this method one second later
        if (this.el.nativeElement.shadowRoot.children.length === 0) {
            setTimeout(() => this.ngOnInit(count + 1), 1000);
            return;
        }

        // assign position value to shadowRoot child
        this.el.nativeElement.shadowRoot.children[1].style.position = 'relative';
    }
}

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.