{"id":28089487,"url":"https://github.com/nonsalant/print-button","last_synced_at":"2025-05-13T12:58:52.427Z","repository":{"id":290290097,"uuid":"928918158","full_name":"nonsalant/print-button","owner":"nonsalant","description":"Web Component to print only a specific element (instead of the whole page)","archived":false,"fork":false,"pushed_at":"2025-04-28T02:43:36.000Z","size":3,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-28T03:29:40.556Z","etag":null,"topics":["button","css","light-dom","print","utility","vanilla-javascript","web-components"],"latest_commit_sha":null,"homepage":"https://codepen.io/nonsalant/pen/yyBmeBp","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nonsalant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-02-07T13:30:01.000Z","updated_at":"2025-04-28T02:43:40.000Z","dependencies_parsed_at":"2025-04-28T03:39:49.152Z","dependency_job_id":null,"html_url":"https://github.com/nonsalant/print-button","commit_stats":null,"previous_names":["nonsalant/print-button"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nonsalant%2Fprint-button","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nonsalant%2Fprint-button/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nonsalant%2Fprint-button/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nonsalant%2Fprint-button/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nonsalant","download_url":"https://codeload.github.com/nonsalant/print-button/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253948391,"owners_count":21988953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["button","css","light-dom","print","utility","vanilla-javascript","web-components"],"created_at":"2025-05-13T12:58:51.890Z","updated_at":"2025-05-13T12:58:52.411Z","avatar_url":"https://github.com/nonsalant.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# print-button\n Web Component to print only a specific element instead of the whole page (which is what `window.print()` does by default).\n\n**[CodePen Demo](https://codepen.io/nonsalant/pen/yyBmeBp)**\n\n## Examples\n\nPrint a specific element on the page:\n```html\n\u003cprint-button print-target=\".element-to-print\"\u003e\n    Print\n\u003c/print-button\u003e\n```\nPrint the whole page:\n```html\n\u003cprint-button\u003ePrint\u003c/print-button\u003e\n```\n\nMultiple print buttons on the same page:\n```html\n\u003cprint-button print-target=\"#table-1\"\u003e\n    Print the 1st table\n\u003c/print-button\u003e\n\u003ctable id=\"table-1\"\u003e...\u003c/table\u003e\n\n\u003cprint-button print-target=\"#table-2\"\u003e\n    Print the 2nd table\n\u003c/print-button\u003e\n\u003ctable id=\"table-2\"\u003e...\u003c/table\u003e\n```\n\nJust the icon:\n```html\n\u003cprint-button print-target=\".element-to-print\"\u003e\u003c/print-button\u003e\n```\n\n## Attributes and Content\n- `print-target` attribute: CSS selector of the element to print. By default (if not provided), it prints the whole page except for the button itself.\n- Content: The text content of the button. If left empty, `aria-label=\"Print\"` is added to the button (which will be a simple icon button).\n\n## Including the script\n\nThe script can be included anywhere on the page... \n- as a module:\n```html\n\u003cscript type=\"module\" src=\"print-button.js\"\u003e\u003c/script\u003e\n```\n- or as a non-module script tag:\n```html\n\u003cscript src=\"print-button.js\"\u003e\u003c/script\u003e\n```\n\n## How it works\nThe `printOnly()` method:\n- starts at the target, \n- adds a `.dont-print` class to its siblings, \n- moves to the parent and adds the class to _its_ siblings, \n- and continues up the DOM recursively adding the `.dont-print` class to all of the \"uncles\" (non-ancestors) of the target element.\n\n## Other notes\nThe `setTimeout(()=\u003e{...})` wrapping the insides of the `connectedCallback()` lifecycle method allows the script to be used as a non-module script tag (in addition to the module option) from anywhere on the page, including the header, by adding a ~4ms delay which should be enough for the DOM to be ready. \u003cbr\u003eIncrease the delay if needed (if your `print-target` takes longer to load), e.g:\n```javascript\nconnectedCallback() {\n    setTimeout(() =\u003e {\n        //...\n    }, 150);\n}\n```\n\n## Credits\n- Print icon from [Ionicons 5: print-sharp](https://github.com/ionic-team/ionicons/blob/main/src/svg/print-sharp.svg).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnonsalant%2Fprint-button","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnonsalant%2Fprint-button","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnonsalant%2Fprint-button/lists"}