Skip to content

Commit

Permalink
feat: add switch
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-ub committed Aug 28, 2024
1 parent ee07559 commit d39d780
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
42 changes: 42 additions & 0 deletions apps/www/src/content/docs/docs/components/switch.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Switch
description: A control that allows the user to toggle between checked and not checked.
links:
doc: https://www.radix-ng.com/?path=/docs/primitives-switch--docs
---

<ComponentPreview name="switch-demo" />

## Installation

import { Tabs, TabItem } from "@astrojs/starlight/components";
import { Steps } from "@astrojs/starlight/components";


<Tabs>
<TabItem label="CLI">
```bash frame="code"
npx shadcn-ng@latest add switch
```
</TabItem>

<TabItem label="Manual">
<Steps>
1. Copy and paste the following code into your project.
<ComponentSource name="switch" />

2. Update the import paths to match your project setup.
</Steps>

</TabItem>
</Tabs>

## Usage

```ts
import { SwitchDirective } from '@/components/ui/switch.directive'
```

```angular-html
<button ubSwitch />
```
19 changes: 19 additions & 0 deletions apps/www/src/registry/default/example/switch-demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component } from '@angular/core';

import { SwitchDirective } from '@/registry/default/ui/switch.directive'
import { UbLabelDirective } from '@/registry/default/ui/label.directive'

@Component({
standalone: true,
selector: 'switch-demo-default',
imports: [SwitchDirective, UbLabelDirective],
template: `
<div class="flex items-center space-x-2">
<button ubSwitch id="airplane-mode"></button>
<label for="airplane-mode">Airplane Mode</label>
</div>
`
})
export class SwitchDemoDefault { }

export default SwitchDemoDefault;
29 changes: 29 additions & 0 deletions apps/www/src/registry/default/ui/switch.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { cn } from '@/lib/utils';
import { Component, computed, input } from '@angular/core';

import { RdxSwitchRootDirective, RdxSwitchThumbDirective } from '@radix-ng/primitives/switch'

@Component({
standalone: true,
selector: 'button[ubSwitch]',
imports: [RdxSwitchThumbDirective],
hostDirectives: [
{
directive: RdxSwitchRootDirective,
inputs: ['id', 'required', 'checked', 'disabled'],
outputs: ['onCheckedChange']
}
],
host: {
'[class]': 'computedClass()'
},
template: `
<span rdxSwitchThumb class="pointer-events-none block h-5 w-5 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-5 data-[state=unchecked]:translate-x-0"></span>
`
})
export class SwitchDirective {
readonly class = input<string>();
readonly computedClass = computed(() => {
return cn('peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input', this.class())
});
}
19 changes: 19 additions & 0 deletions apps/www/src/registry/new-york/example/switch-demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component } from '@angular/core';

import { SwitchDirective } from '@/registry/new-york/ui/switch.directive'
import { UbLabelDirective } from '@/registry/new-york/ui/label.directive'

@Component({
standalone: true,
selector: 'switch-demo-new-york',
imports: [SwitchDirective, UbLabelDirective],
template: `
<div class="flex items-center space-x-2">
<button ubSwitch id="airplane-mode"></button>
<label for="airplane-mode">Airplane Mode</label>
</div>
`
})
export class SwitchDemoNewYork { }

export default SwitchDemoNewYork;
29 changes: 29 additions & 0 deletions apps/www/src/registry/new-york/ui/switch.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { cn } from '@/lib/utils';
import { Component, computed, input } from '@angular/core';

import { RdxSwitchRootDirective, RdxSwitchThumbDirective } from '@radix-ng/primitives/switch'

@Component({
standalone: true,
selector: 'button[ubSwitch]',
imports: [RdxSwitchThumbDirective],
hostDirectives: [
{
directive: RdxSwitchRootDirective,
inputs: ['id', 'required', 'checked', 'disabled'],
outputs: ['onCheckedChange']
}
],
host: {
'[class]': 'computedClass()'
},
template: `
<span rdxSwitchThumb class="pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0"></span>
`
})
export class SwitchDirective {
readonly class = input<string>();
readonly computedClass = computed(() => {
return cn('peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input', this.class())
});
}
8 changes: 7 additions & 1 deletion apps/www/src/registry/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const ui: Registry = [
{
name: "accordion",
type: "components:ui",
dependencies: ["@ng-icons/core", "@ng-icons/lucide"],
dependencies: ["@ng-icons/core", "@ng-icons/lucide", "@radix-ng/primitives"],
files: ["ui/accordion.directive.ts"],
},
{
Expand Down Expand Up @@ -54,6 +54,12 @@ export const ui: Registry = [
type: "components:ui",
files: ["ui/skeleton.directive.ts"],
},
{
name: "switch",
type: "components:ui",
dependencies: ["@radix-ng/primitives"],
files: ["ui/switch.directive.ts"],
},
{
name: "table",
type: "components:ui",
Expand Down

0 comments on commit d39d780

Please sign in to comment.